分页
limit :在命令的最后面
1. 限制查询出来的数据个数
eg:select * from students where gender=1 limit 2; 限制查询出来的数据为2个
2. 查询从几开始的定额数据
eg:select * from students where gender=1 limit 0 ,5; 查询第一页的5个数据
连接查询
1. 内连接查询:inner join
select ... from 表1 inner join 表2;将两个数据表相乘
select ... from 表1 inner join 表2 on 表1. _ =表2._; 两个表内连接
eg:
select * from students inner join classes on students.clsid=classes.id; * 会使两个表里的所有数据显示
select students.name,classes.name from students inner join classes on students.clsid=classes.id;
select s.name,c.name from students as s inner join classes as c on s.clsid=c.id; 更换表名
select s.*,c.name from students as s inner join classes as c on s.clsid=c.id;
select students. *,classes.name from students inner join classes on students.clsid=classes.id order by classes.name,students.id; 排序
2. 外连接
left join 以左表为基准显示
right join 以右表为基准显示(与left join 同用)
连接后产生一个新表,可以用having进行筛选。(where也行,写在having的位置)
自关联
一个表里的字段关联这个表里的另一个字段
1. 创建areas表:
create table areas{
aid int primary key,
atitle varchar(20),
pid int
};
2.将虚拟机终端定位到areas表的位置,然后登陆mysql
source 数据表名称 ; 导入数据表
3. areas表的内连接:
select * from areas as province inner join areas as city on city.pid=province.aid having province.attile ="山东省";山东省的地级市
子查询的结果可当作一个数据表进行操作
eg: select * from (select catename,max(price)as maxprice from goods group by catename )as goodsnew left join goods as good on goodnews.catename=good.catename and goodnews.maxprice=good.price order by goodsnew.catename;