Sql语句之select 5种查询
select 5種子句:注意順序
where / group by /having / order by / limit /
清空表中的數據:truncate 表名;
導入表結構(不含數據): create table 表2 like 表1;
刪除表:drop table 表名;
導入數據:insert into g2 select * from stu order by name, fenshu desc;
//從臨時表中查詢=========子查詢
select * from (select * from stu order by name, fenshu desc) as tmp group by name;
where 表達式 把表達式放在行中,看表達式是否為真;
列 當成 “變量” 來理解 可以運算
查詢結果 當成“臨時表” 來理解 
子查詢:3種:
where型子查詢:把內層的查詢結果作為外層子查詢的條件
 select * from stu where fenshu=(select max(fenshu) from stu);
 select * from stu where fenshu in (select max(fenshu) from stu group by name);
from 型子查詢:把內層的查詢結果當成臨時表供外層繼續查詢:
select * from (select * from stu order by name, fenshu desc) as tmp group by name; //必須有as tmp做別名,否則報錯;
exists型子查詢:(難點)
 把外層的查詢結果拿到內層,看內層的查詢是否成立;
select * from stu where exists (select * from goods where goods.cat_id = category.cat_id);
select 查詢5子句之order by:
根據字段進行排序而已:
若要倒序排列則用“desc”來聲明一下即可
顯示聲明升序排列用“asc”來聲明;
select name, fenshu from stu order by fenshu, name desc;
limit關鍵字;起到限制條目作用;
limit [offset],N
offset:偏移量, 默認是0;
N:取出的條目
取第3行之后的4行;
select * from stu order by fenshu desc limit 3, 4;
?
select 查詢5子句之having查詢:(用于在緩沖區的查詢,而不能在表中(即mysql的文件)查詢)
select good_id, goods_name, market_price - shop_price as sheng from goods where
market_price > 200;
select good_id, goods_name, market_price -shop_price as sheng from goods having
sheng > 200;
select good_id, goods_name, market_price -shop_price as sheng from goods where cat_id = 3 having sheng >200;
select cat_id.sum(shop*goods_number) as huokuan from group by cat_id;
select cat_id.sum(shop*goods_number) as huokuan from group by cat_id having huokuan > 20000;
#每個人的平均分
select name, avg(scores) from stu group by name;
#每個人的掛科情況;
select name.scores < 60 from stu;
#每個人的掛科科目數目:
select name, sum(score < 60) from stu group by name;
select name, sum(score<60), avg(scores) as pj from stu group by name;
?
總結
以上是生活随笔為你收集整理的Sql语句之select 5种查询的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: RabbitMQ学习二
- 下一篇: Rails当你运行一个数据库回滚错误:A
