在MySQL查詢山東省男生信息_MySQL-查詢

來一波英語單詞解釋(意思)

create ? 創建

show? 顯示

database ? 數據庫

use? ? 使用

select ? 選擇

table ? 表

from ? 來自…

distinct ? 消除重復行

as ? 同樣地(用于其別名)

where? 范圍

like ? 模糊查詢

rlike? 正則查詢

In ? 范圍查詢

not in 不非連續的范圍之內

between ... and …表示在一個連續的范圍內

not between ... and ...表示不在一個連續的范圍內

is null ? 判為空

is not null ? 判非空

order by ? 字段

asc從小到大排列,即升序

desc從大到小排序,即降序

count ? 總數

max ? 最大值

min ? 最小值

sum ? 求和

avg ? 平均值

round ? 四舍五入

group by? 分組

group_concat ? 字符串連接使用

having ? 篩選分組的數據

limit? ? 分頁

inner join(等值連接) 只返回兩個表中聯結字段相等的行

left join(左聯接) 返回包括左表中的所有記錄和右表中聯結字段相等的記錄

right join(右聯接) 返回包括右表中的所有記錄和左表中聯結字段相等的記錄

-- 數據的準備

-- 創建一個數據庫

create database python_test charset=utf8;

-- 使用一個數據庫

use python_test;

-- 顯示使用的當前數據是哪個?

select database();

-- 創建一個數據表

-- students表

create table students(

id int unsigned primary key auto_increment not null,

name varchar(20) default '',

age tinyint unsigned default 0,

height decimal(5,2),

gender enum('男','女','中性','保密') default '保密',

cls_id int unsigned default 0,

is_delete bit default 0

);

-- classes表

create table classes (

id int unsigned auto_increment primary key not null,

name varchar(30) not null

);

insert into students (name,age) values("趙日天",9);

insert into students set name = "李殺神",age=10;

-- 查詢

-- 查詢所有字段

-- select * from 表名;

select * from students;

select * from classes;

select id, name from classes;

-- 查詢指定字段

-- select 列1,列2,... from 表名;

select name, age from students;

-- 使用 as 給字段起別名

-- select 字段 as 名字.... from 表名;

select name as 姓名, age as 年齡 from students;

-- select 表名.字段 .... from 表名;

select students.name, students.age from students;

-- 可以通過 as 給表起別名

-- select 別名.字段 .... from 表名 as 別名;

select students.name, students.age from students;

select s.name, s.age from students as s;

-- 消除重復行

-- distinct 字段

select distinct gender from students;

-- 條件查詢

-- 比較運算符

-- select .... from 表名 where .....

-- >

-- 查詢大于18歲的信息

select * from students where age>18;

select id,name,gender from students where age>18;

-- <

-- 查詢小于18歲的信息

select * from students where age<18;

-- >=

-- <=

-- 查詢小于或者等于18歲的信息

-- =

-- 查詢年齡為18歲的所有學生的名字

select * from students where age=18;

-- != 或者 <>

-- 邏輯運算符

-- and

-- 18到28之間的所以學生信息

select * from students where age>18 and age<28;

-- 18歲以上的女性

select * from students where age>18 and gender="女";

select * from students where age>18 and gender=2;

-- or

-- 18以上或者身高查過180(包含)以上

select * from students where age>18 or height>=180;

-- not

-- 不在 18歲以上的女性 這個范圍內的信息

-- select * from students where not age>18 and gender=2;

select * from students where not (age>18 and gender=2);

-- 年齡不是小于或者等于18 并且是女性

select * from students where (not age<=18) and gender=2;

-- 模糊查詢

-- like

-- % 替換1個或者多個

-- _ 替換1個

-- 查詢姓名中 以 "小" 開始的名字

select name from students where name="小";

select name from students where name like "小%";

-- 查詢姓名中 有 "小" 所有的名字

select name from students where name like "%小%";

-- 查詢有2個字的名字

select name from students where name like "__";

-- 查詢有3個字的名字

select name from students where name like "__";

-- 查詢至少有2個字的名字

select name from students where name like "__%";

-- rlike 正則

-- 查詢以 周開始的姓名

select name from students where name rlike "^周.*";

-- 查詢以 周開始、倫結尾的姓名

select name from students where name rlike "^周.*倫$";

-- 范圍查詢

-- in (1, 3, 8)表示在一個非連續的范圍內

-- 查詢 年齡為18、34的姓名

select name,age from students where age=18 or age=34;

select name,age from students where age=18 or age=34 or age=12;

select name,age from students where age in (12, 18, 34);

-- not in 不非連續的范圍之內

-- 年齡不是 18、34歲之間的信息

select name,age from students where age not in (12, 18, 34);

-- between ... and ...表示在一個連續的范圍內

-- 查詢 年齡在18到34之間的的信息

select name, age from students where age between 18 and 34;

-- not between ... and ...表示不在一個連續的范圍內

-- 查詢 年齡不在在18到34之間的的信息

select * from students where age not between 18 and 34;

select * from students where not age between 18 and 34;

-- 空判斷

-- 判空is null

-- 查詢身高為空的信息

select * from students where height is null;

select * from students where height is NULL;

select * from students where height is Null;

-- 判非空is not null

select * from students where height is not null;

-- 排序

-- order by 字段

-- asc從小到大排列,即升序

-- desc從大到小排序,即降序

-- 查詢年齡在18到34歲之間的男性,按照年齡從小到到排序

select * from students where (age between 18 and 34) and gender=1;

select * from students where (age between 18 and 34) and gender=1 order by age;

select * from students where (age between 18 and 34) and gender=1 order by age asc;

-- 查詢年齡在18到34歲之間的女性,身高從高到矮排序

select * from students where (age between 18 and 34) and gender=2 order by height desc;

-- order by 多個字段

-- 查詢年齡在18到34歲之間的女性,身高從高到矮排序, 如果身高相同的情況下按照年齡從小到大排序

select * from students where (age between 18 and 34) and gender=2 order by height desc,id desc;

-- 查詢年齡在18到34歲之間的女性,身高從高到矮排序, 如果身高相同的情況下按照年齡從小到大排序,

-- 如果年齡也相同那么按照id從大到小排序

select * from students where (age between 18 and 34) and gender=2 order by height desc,age asc,id desc;

-- 按照年齡從小到大、身高從高到矮的排序

select * from students order by age asc, height desc;

-- 聚合函數

-- 總數

-- count

-- 查詢男性有多少人,女性有多少人

select * from students where gender=1;

select count(*) from students where gender=1;

select count(*) as 男性人數 from students where gender=1;

select count(*) as 女性人數 from students where gender=2;

-- 最大值

-- max

-- 查詢最大的年齡

select age from students;

select max(age) from students;

-- 查詢女性的最高 身高

select max(height) from students where gender=2;

-- 最小值

-- min

-- 求和

-- sum

-- 計算所有人的年齡總和

select sum(age) from students;

-- 平均值

-- avg

-- 計算平均年齡

select avg(age) from students;

-- 計算平均年齡 sum(age)/count(*)

select sum(age)/count(*) from students;

-- 四舍五入 round(123.23 , 1) 保留1位小數

-- 計算所有人的平均年齡,保留2位小數

select round(sum(age)/count(*), 2) from students;

select round(sum(age)/count(*), 3) from students;

-- 計算男性的平均身高 保留2位小數

select round(avg(height), 2) from students where gender=1;

-- select name, round(avg(height), 2) from students where gender=1;

-- 分組

-- group by

-- 按照性別分組,查詢所有的性別

select name from students group by gender;

select * from students group by gender;

select gender from students group by gender;

-- 計算每種性別中的人數

select gender,count(*) from students group by gender;

-- 計算男性的人數

select gender,count(*) from students where gender=1 group by gender;

-- group_concat(...)

-- 查詢同種性別中的姓名

select gender,group_concat(name) from students where gender=1 group by gender;

select gender,group_concat(name, age, id) from students where gender=1 group by gender;

select gender,group_concat(name, "_", age, " ", id) from students where gender=1 group by gender;

-- having

-- 查詢平均年齡超過30歲的性別,以及姓名 having avg(age) > 30

select gender, group_concat(name),avg(age) from students group by gender having avg(age)>30;

-- 查詢每種性別中的人數多于2個的信息

select gender, group_concat(name) from students group by gender having count(*)>2;

-- 分頁

-- limit start, count

-- 限制查詢出來的數據個數

select * from students where gender=1 limit 2;

-- 查詢前5個數據

select * from students limit 0, 5;

-- 查詢id6-10(包含)的書序

select * from students limit 5, 5;

-- 每頁顯示2個,第1個頁面

select * from students limit 0,2;

-- 每頁顯示2個,第2個頁面

select * from students limit 2,2;

-- 每頁顯示2個,第3個頁面

select * from students limit 4,2;

-- 每頁顯示2個,第4個頁面

select * from students limit 6,2; -- -----> limit (第N頁-1)*每個的個數, 每頁的個數;

-- 每頁顯示2個,顯示第6頁的信息, 按照年齡從小到大排序

select * from students order by age asc limit 10,2;

select * from students where gender=2 order by height desc limit 0,2;

-- 連接查詢

-- inner join ... on

-- select ... from 表A inner join 表B;

select * from students inner join classes;

-- 查詢 有能夠對應班級的學生以及班級信息

select * from students inner join classes on students.cls_id=classes.id;

-- 按照要求顯示姓名、班級

select students.*, classes.name from students inner join classes on students.cls_id=classes.id;

select students.name, classes.name from students inner join classes on students.cls_id=classes.id;

-- 給數據表起名字

select s.name, c.name from students as s inner join classes as c on s.cls_id=c.id;

-- 查詢 有能夠對應班級的學生以及班級信息,顯示學生的所有信息,只顯示班級名稱

select s.*, c.name from students as s inner join classes as c on s.cls_id=c.id;

-- 在以上的查詢中,將班級姓名顯示在第1列

select c.name, s.* from students as s inner join classes as c on s.cls_id=c.id;

-- 查詢 有能夠對應班級的學生以及班級信息, 按照班級進行排序

select c.name, s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name;

-- 當時同一個班級的時候,按照學生的id進行從小到大排序

select c.name, s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name,s.id;

-- left join

-- 查詢每位學生對應的班級信息

select * from students as s left join classes as c on s.cls_id=c.id;

-- 查詢沒有對應班級信息的學生

-- select ... from xxx as s left join xxx as c on..... where .....

-- select ... from xxx as s left join xxx as c on..... having .....

select * from students as s left join classes as c on s.cls_id=c.id having c.id is null;

select * from students as s left join classes as c on s.cls_id=c.id where c.id is null;

-- right join on

-- 將數據表名字互換位置,用left join完成

-- 自關聯

-- 查詢所有省份

select * from areas where pid is null;

-- 查詢出山東省有哪些市

select * from areas as province inner join areas as city on city.pid=province.aid having province.atitle="山東省";

select province.atitle, city.atitle from areas as province inner join areas as city on city.pid=province.aid having province.atitle="山東省";

-- 查詢出青島市有哪些縣城

select province.atitle, city.atitle from areas as province inner join areas as city on city.pid=province.aid having province.atitle="青島市";

select * from areas where pid=(select aid from areas where atitle="青島市")

-- 子查詢

-- 標量子查詢

-- 查詢出高于平均身高的信息

-- 查詢最高的男生信息

select * from students where height = 188;

select * from students where height = (select max(height) from students);

-- 列級子查詢

-- 查詢學生的班級號能夠對應的學生信息

-- select * from students where cls_id in (select id from classes);

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/530001.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/530001.shtml
英文地址,請注明出處:http://en.pswp.cn/news/530001.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

java 導入world數據_java讀取world文件,把world文件中的內容,原樣輸出到頁面上。...

POI,處理可以。樣式在Java代碼中添加就可以。給了一個例子這個是Excel的。package cn.com.my.common;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.sql.Connection;import java.sql.ResultSet…

java程序員 css_Java程序員從笨鳥到菜鳥之(十七)CSS基礎積累總結(下)

七.組織元素(span和div)span和div元素用于組織和結構化文檔&#xff0c;并經常聯合class和id屬性一起使用。在這一課中&#xff0c;我們將進一步探究span和div的用法&#xff0c;因為這兩個HTML元素對于CSS是很重要的。用span組織元素用div組織元素用span組織元素span元素可以說…

redlock java_Redlock分布式鎖

這篇文章主要是對 Redis 官方網站刊登的 Distributed locks with Redis 部分內容的總結和翻譯。什么是 RedLockRedis 官方站這篇文章提出了一種權威的基于 Redis 實現分布式鎖的方式名叫 Redlock&#xff0c;此種方式比原先的單節點的方法更安全。它可以保證以下特性&#xff1…

java 兩個數組交叉_java – 如何交叉兩個沒有重復的排序整數數組?

這個問題本質上減少到一個連接操作,然后是一個過濾器操作(刪除重復,只保留內部匹配).由于輸入都已經排序,所以可以通過O(O(size(a)size(b))的merge join來有效地實現連接.過濾器操作將為O(n),因為連接的輸出被排序,并且要刪除重復項,所有您需要做的是檢查每個元素是否與之??前…

java retentionpolicy_Java注解之如何利用RetentionPolicy.SOURCE生存周期

上一篇文章簡單講了下Java注解的學習之元注解說明&#xff0c;學習了Java注解是如何定義的&#xff0c;怎么使用的&#xff0c;但是并沒有介紹Java的注解是怎么起作用的&#xff0c;像Spring Boot里面的那些注解&#xff0c;到底是怎么讓程序這樣子運行起來的&#xff1f;特別是…

在java程序中定義的類有兩種成員_java試題 急需答案 謝謝!!!

三、填空(每小題2分&#xff0c;共10分)1&#xff0e;在Applet中&#xff0c;創建一個具有10行45列的多行文本區對象ta的語句為&#xff1a;2&#xff0e;創建一個標識有“關閉”字樣的標簽對象gb的語句為。3&#xff0e;方法是一種僅有方法頭&#xff0c;沒...三、填空(每小題…

java 同步 變量,在java中的對象上同步,然后更改同步的變量的值

I came across a code like thissynchronized(obj) {obj new Object();}Something does not feel right about this , I am unable to explain, Is this piece of code OK or there is something really wrong in it, please point it out.Thanks解決方案Its probably not wha…

java set泛型_Java 集合二 泛型、Set相關

泛型1、在定義一個類的方法時&#xff0c;因為不確定返回值類型&#xff0c;所以用一個符號代替&#xff0c;這個符號就是泛型eg:ArrayList list new ArrayList();2、泛型的好處&#xff1a;1、提高了數據的安全性&#xff0c;將運行時的問題提前暴露在編譯階段2、避免了強轉的…

java annotation 實現_在Java中如何實現自己的annotation

1. 先定義annotation2. 使用annotation例子&#xff1a;import java.lang.annotation.*;import java.lang.reflect.Method;Target(ElementType.METHOD)Retention(RetentionPolicy.RUNTIME)interface Test {String info() default "";}class Annotated {Test(info &q…

登錄界面攔截java_java攔截通過url訪問頁面,必須通過登錄頁面訪問目標頁面

在web.xml中配置過濾&#xff1a;LoginFiltercom.verification.action.LoginFilterLoginFiltery/form/dealParse.do/* 攔截所有請求/.do 攔截以“.do”結尾的請求/index.jsp 攔截指定的jsp/artery/form/* 攔截該目錄下的所有請求等等攔截器&#xff0c;攔截請求類&#xf…

python textwrap_[Python標準庫]textwrap——格式化文本段落

textwrap——格式化文本段落作用&#xff1a;通過調整換行符在段落中出現的位置來格式化文本。 Python 版本&#xff1a;2.5 及以后版本 需要美觀打印時&#xff0c;可以用 textwrap 模塊來格式化要輸出的文本。這個模塊允許通過編程提供類似段落自動換行或填充…

java 字符串 1_java 字符串操作大全1

1、length() 字符串的長度例&#xff1a;char chars[]{a,b.c};String snew String(chars);int lens.length();2、charAt() 截取一個字符例&#xff1a;char ch;ch"abc".charAt(1); 返回b3、getChars() 截取多個字符void getChars(int sourceStart,int sourceEnd,char…

java實現權限_Java實現權限管理的兩種方式

編輯特別推薦:種方式&#xff1a;利用filter、xml文件和用戶信息表配合使用來實現權限管理。1.過濾器filterpackage cn.com.aaa.bbb.filter;import java.io.IOException;import java.io.InputStream;import java.util.HashMap;import java.util.Iterator;import java.util.List…

java 輸入16進制_嘗試使用十六進制輸入來使用小端和大端

我試圖用這兩個原型編寫C函數&#xff1a;int extract_little (char* str, int ofset, int n);int extract_big(char* str, int ofset, int n);現在一般的想法是我需要從地址str ofset開始以兩種格式返回一個n字節整數 . 附&#xff1a; Ofset還沒有做任何事情&#xff0c;我計…

java gson_Java 中 Gson的使用

JSON 是一種文本形式的數據交換格式&#xff0c;它比XML更輕量、比二進制容易閱讀和編寫&#xff0c;調式也更加方便;解析和生成的方式很多&#xff0c;Java中最常用的類庫有&#xff1a;JSON-Java、Gson、Jackson、FastJson等一、Gson的基本用法Gson提供了fromJson() 和toJson…

spring注入普通java類_普通java類如何取得注入spring Ioc容器的對象

[除了使用XML配置外&#xff0c;還可以選擇使用基于注解(annotation)的配置方式&#xff0c;其依賴于字節碼來織入組件。注解注入在XML注入之前完成&#xff0c;因此在XML配置中可以重載注解注入的屬性。一、建一個SpringUtil類package com.ceopen.eoss.spring; import org.spr…

java web 集成dom4j_[JavaWeb基礎] 031.dom4j寫入xml的方法

上一篇我們講述了dom4j讀取xml的4種方法&#xff0c;甚是精彩&#xff0c;那么怎么樣寫入xml呢&#xff1f;我們直接看下源碼實現。public static void main(String[] args) throws Exception {// 創建文檔Document document DocumentHelper.createDocument();// 設置編碼docu…

java servlet 調試日志 logger sae_java servlet 調試日志 lo

java servlet 調試日志 lo[2021-02-10 08:32:08] 簡介:php去除nbsp的方法&#xff1a;首先創建一個PHP代碼示例文件&#xff1b;然后通過“preg_replace("/(\s|\&nbsp\;| |\xc2\xa0)/", " ", strip_tags($val));”方法去除所有nbsp即可。推薦&#x…

java接口權限管理在哪里_java訪問權限控制

為什么java要有訪問權限的控制?訪問權限的設置和代碼的重構有關。在一個項目中&#xff0c;大多數的時間和金錢都投入到了代碼的維護當中。維護中一定會修改已存在的不合理的代碼。但是在重構的過程中&#xff0c;就出現了這樣的問題&#xff1a;如何保證不影響那些使用了待修…

java8 stream index_Java8的stream用法整理

/***authorindex* date 2020/10/27**/public classTestcollectingAndThen {Testpublic voidtest(){final int NUM 14;List peopleList new ArrayList<>(NUM);String[] names {"小張", "小龍", "小牛", "小豬", "小黑&quo…