日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

在MySQL查询山东省男生信息_MySQL-查询

發(fā)布時(shí)間:2025/3/8 数据库 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在MySQL查询山东省男生信息_MySQL-查询 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

create ? 創(chuàng)建

show? 顯示

database ? 數(shù)據(jù)庫(kù)

use? ? 使用

select ? 選擇

table ? 表

from ? 來自…

distinct ? 消除重復(fù)行

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

where? 范圍

like ? 模糊查詢

rlike? 正則查詢

In ? 范圍查詢

not in 不非連續(xù)的范圍之內(nèi)

between ... and …表示在一個(gè)連續(xù)的范圍內(nèi)

not between ... and ...表示不在一個(gè)連續(xù)的范圍內(nèi)

is null ? 判為空

is not null ? 判非空

order by ? 字段

asc從小到大排列,即升序

desc從大到小排序,即降序

count ? 總數(shù)

max ? 最大值

min ? 最小值

sum ? 求和

avg ? 平均值

round ? 四舍五入

group by? 分組

group_concat ? 字符串連接使用

having ? 篩選分組的數(shù)據(jù)

limit? ? 分頁

inner join(等值連接) 只返回兩個(gè)表中聯(lián)結(jié)字段相等的行

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

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

-- 數(shù)據(jù)的準(zhǔn)備

-- 創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)

create database python_test charset=utf8;

-- 使用一個(gè)數(shù)據(jù)庫(kù)

use python_test;

-- 顯示使用的當(dāng)前數(shù)據(jù)是哪個(gè)?

select database();

-- 創(chuàng)建一個(gè)數(shù)據(jù)表

-- 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;

-- 消除重復(fù)行

-- distinct 字段

select distinct gender from students;

-- 條件查詢

-- 比較運(yùn)算符

-- 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歲的所有學(xué)生的名字

select * from students where age=18;

-- != 或者 <>

-- 邏輯運(yùn)算符

-- and

-- 18到28之間的所以學(xué)生信息

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歲以上的女性 這個(gè)范圍內(nèi)的信息

-- 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個(gè)或者多個(gè)

-- _ 替換1個(gè)

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

select name from students where name="小";

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

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

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

-- 查詢有2個(gè)字的名字

select name from students where name like "__";

-- 查詢有3個(gè)字的名字

select name from students where name like "__";

-- 查詢至少有2個(gè)字的名字

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

-- rlike 正則

-- 查詢以 周開始的姓名

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

-- 查詢以 周開始、倫結(jié)尾的姓名

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

-- 范圍查詢

-- in (1, 3, 8)表示在一個(gè)非連續(xù)的范圍內(nèi)

-- 查詢 年齡為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 不非連續(xù)的范圍之內(nèi)

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

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

-- between ... and ...表示在一個(gè)連續(xù)的范圍內(nèi)

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

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

-- not between ... and ...表示不在一個(gè)連續(xù)的范圍內(nèi)

-- 查詢 年齡不在在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 多個(gè)字段

-- 查詢年齡在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;

-- 聚合函數(shù)

-- 總數(shù)

-- count

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

select * from students where gender=1;

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

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

select count(*) as 女性人數(shù) 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

-- 計(jì)算所有人的年齡總和

select sum(age) from students;

-- 平均值

-- avg

-- 計(jì)算平均年齡

select avg(age) from students;

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

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

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

-- 計(jì)算所有人的平均年齡,保留2位小數(shù)

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

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

-- 計(jì)算男性的平均身高 保留2位小數(shù)

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;

-- 計(jì)算每種性別中的人數(shù)

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

-- 計(jì)算男性的人數(shù)

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;

-- 查詢每種性別中的人數(shù)多于2個(gè)的信息

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

-- 分頁

-- limit start, count

-- 限制查詢出來的數(shù)據(jù)個(gè)數(shù)

select * from students where gender=1 limit 2;

-- 查詢前5個(gè)數(shù)據(jù)

select * from students limit 0, 5;

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

select * from students limit 5, 5;

-- 每頁顯示2個(gè),第1個(gè)頁面

select * from students limit 0,2;

-- 每頁顯示2個(gè),第2個(gè)頁面

select * from students limit 2,2;

-- 每頁顯示2個(gè),第3個(gè)頁面

select * from students limit 4,2;

-- 每頁顯示2個(gè),第4個(gè)頁面

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

-- 每頁顯示2個(gè),顯示第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;

-- 查詢 有能夠?qū)?yīng)班級(jí)的學(xué)生以及班級(jí)信息

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

-- 按照要求顯示姓名、班級(jí)

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;

-- 給數(shù)據(jù)表起名字

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

-- 查詢 有能夠?qū)?yīng)班級(jí)的學(xué)生以及班級(jí)信息,顯示學(xué)生的所有信息,只顯示班級(jí)名稱

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

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

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

-- 查詢 有能夠?qū)?yīng)班級(jí)的學(xué)生以及班級(jí)信息, 按照班級(jí)進(jìn)行排序

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

-- 當(dāng)時(shí)同一個(gè)班級(jí)的時(shí)候,按照學(xué)生的id進(jìn)行從小到大排序

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

-- 查詢每位學(xué)生對(duì)應(yīng)的班級(jí)信息

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

-- 查詢沒有對(duì)應(yīng)班級(jí)信息的學(xué)生

-- 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

-- 將數(shù)據(jù)表名字互換位置,用left join完成

-- 自關(guān)聯(lián)

-- 查詢所有省份

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="青島市")

-- 子查詢

-- 標(biāo)量子查詢

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

-- 查詢最高的男生信息

select * from students where height = 188;

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

-- 列級(jí)子查詢

-- 查詢學(xué)生的班級(jí)號(hào)能夠?qū)?yīng)的學(xué)生信息

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

總結(jié)

以上是生活随笔為你收集整理的在MySQL查询山东省男生信息_MySQL-查询的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。