mysql select

select a, b, t1.c, a*b as d

from t1, db.t2

where a between 3 and 7 or c not in (5,100,124)

group by ...

having ...

order by d desc, e select的列里没有e

limit 5; 等价于limit 0,5 和 limit 5 offset 0

 

select distinct a,b from table; 去重,放在所有列之前,作用于所有列,

a、b两列都相同才视为相同

 

select concat( rtrim(a), ' (', b, ')' ) as c 连接、去空格、别名

 

select a*b + - * /

 

子查询

子查询作为where条件,多与in配合使用,也可以和=或<>配合使用

where a in (select a from t where b=3) as t2;

 

 

子查询作为计算字段

select cname, (select count(*)

from orders

where orders.cid=customers.cid) as orders

from customers;

 

子查询作为from字段

select a from (select ...) temp //不加别名报错

 

 

union

列必须相同,但次序可以不同

列数据类型必须兼容,可以隐式转换

order by只能在最后一个select后面出现一次,对全部数据排序

 

select a from t where a>2

union

select a from t where a<6

 

若存在1行a=3,只会被返回一次,union会自动去重

union all 返回所有行

 

select省略from,可用来处理表达式:select 3*2、select now() 等

 

posted @ 2020-05-27 19:54  是的哟  阅读(179)  评论(0)    收藏  举报