-- 查詢數據1 查詢表中的所有數據
-- * 通配符 代表所有字段
-- select * from 表名
select * from student;
-- 查詢數據2 帶條件式的查詢
-- select * from 表名 where 條件表達式
select * from student where sex=2;
select * from student where sex=2 and age = 17;
-- 查詢數據3 指定字段查詢 (推薦 .. 效率高, 目的明確)
-- select `字段名1`, `字段名2`, .... `字段名N` from 表名 [ where 條件表達式 ]
select id,name, age, sex from student where id > 2;
-- 排序查詢
-- select 字段名 ... from 表名 [wherer ...] order by 字段名 asc(默認升序)/desc(降序)
select id,name,sex,age from student order by age;
-- 分組查詢
-- select 字段名 from 表名 group by 字段名
select `class`, max(`score`) from student group by `class`;
-- 嵌套查詢
-- 查詢每個班里面成績最高的人
-- select `class`, name, max(`score`) from student group by `class`;
-- 查詢每個班的最大成績
select max(score) from student group by class;
-- 查詢in條件里每個人的班級,姓名,成績
select `class`,`name`,`score` from student where score in (90,59);
-- 把兩個查詢條件嵌套使用
select `class`,`score`,`name` from student where score in ( select max(score) from student group by class );
-- 多表查詢
select name, hobby from student , life where sid = studentId
-- 給不同的表 取別名(小名) 主要為了 區分 不同的表里有相同的字段名
select s.id, name, sex, hobby
from student s, life l
where s.sid = l.studentId and sex=2
-- 分頁查詢
-- limit rows
-- limit offset,rows
-- offset 偏移量(從哪開始)
-- rows 行數(顯示多少條數據)
select id,name,sex,regtime
from student
limit 3;
select id,name,sex,regtime
from student
limit 2,3;
select id,name,sex,regtime
from student
limit 0,3;
create table if not exists `life`(
`id` int unsigned auto_increment primary key,
`studentId` int(10) unsigned comment '學號',
`hobby` varchar(20) default null comment '愛好'
)engine=MyISAM default charset=utf8;
insert into life values
(null,1,'唱歌'),
(null,1,'嫖'),
(null,1,'賭'),
(null,1,'抽'),
(null,1,'吹簫'),
(null,3,'學霸'),
(null,3,'拍片'),
(null,3,'擼'),
(null,3,'美女'),
(null,6,'看片'),
(null,6,'打手槍'),
(null,6,'打飛機'),
(null,6,'學習PHP')
查詢數據3種方法,排序查詢,分組查詢,嵌套查詢,多表查詢,分頁查詢
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
推薦閱讀更多精彩內容
- 1,先實現多表查詢: (可以發現兩個集合發生了乘積,這叫笛卡爾積問題。) 消除笛卡爾積: (這只是消除了顯示的笛卡...
- 【蝴蝶效應】 蝴蝶效應:上個世紀70年代,美國一個名叫洛倫茲的氣象學家在解釋空氣系統理論時說,亞馬遜雨林一只蝴蝶...