官方文档阅读笔记




  • 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";   #注意系统关键字



你可能感兴趣的:(官方文档阅读笔记)