官方文档阅读笔记




  • Subqueries with EXISTS or NOT EXISTS

If a subquery returns any rows at all, EXISTS subquery is TRUE, and NOT EXISTS subquery is FALSE. For example:

SELECT column1 FROM t1 WHERE EXISTS (SELECT * FROM t2);

Traditionally, an EXISTS subquery starts with SELECT *, but it could begin with SELECT 5 or SELECT column1 or anything at all. MySQL ignores the SELECT list in such a subquery, so it makes no difference.

For the preceding example, if t2 contains any rows, even rows with nothing but NULL values, the EXISTS condition is TRUE. This is actually an unlikely example because a [NOT] EXISTS subquery almost always contains correlations. Here are some more realistic 


  • SELECT * FROM pet WHERE name LIKE '_____';    #查找name是5个字母的


  • SELECT owner, COUNT(*) FROM pet   #查找每个人有多少个记录;


  • 自定义变量

mysql> SELECT @min_price:=MIN(price),@max_price:=MAX(price) FROM shop; 
mysql> SELECT * FROM shop WHERE price=@min_price OR price=@max_price;

   


ORDER BY 3    #按照第三列排序
 select * from table order by length(code), code;   #先以字段长度排序;
 select number from (table) order by (number+0);

The (field + 0 ) converts the text/character in the field into an integer.


"SELECT * FROM `table` ORDER BY `column` DESC";   #注意系统关键字



转载于:https://my.oschina.net/sunpy/blog/611909

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值