1.多表查询
select * from user u,card c where u.userId=c.userId
2.嵌套查询
select * from user where city =(select city from user where userId='101' )
select * from user where city in (select city from user where userId !='101')
SELECT * from user where credit > any(select credit from `user` where city='北京')
SELECT * from user where credit > all(select credit from `user` where city='北京')
SELECT * from user where credit >300 union SELECT * from user where city='北京'
SELECT * from user where credit >300 union all SELECT * from user where city='北京'
select * from 表 where 字段 = (select 字段 from 表)
select * from 表 where 字段 in (select 字段 from 表)
select * from 表 where 字段 > any (select 字段 from 表)
select * from 表 where 字段 > all (select 字段 from 表)
select 字段,(select 字段 from 表2 where 条件) from 表1
select * from (select 字段 字段2 字段3 from 表)t1,(select 字段 字段2 字段3 from 表)t2
where t1.字段=t2.字段
select * from (select 字段 字段2 字段3 from 表)t1,table where t1.字段=table.字段
3.逻辑函数
select *,if(credit>100,'优秀','中等') from user
select *,IFNULL(ename,'--') from user
select *,(case when credit>300 then '优质客户' when credit>200 then '中等客户' else '一般客户' end) from user
select *,(case city when '北京' then '首都' when '上海' then '魔都' when '深圳' then '特区' else '省会' end) from user
4.字符函数。第一个位置被标记为1
select userId, concat(name,','sex) from user
select userId,name,length(name) from user
select name,char_length(name) from user
select name,locate('晓',name) from user
select ename,locate('o',ename,2) from user
select name,lpad(name,3,'#') from user
select name,rpad(name,3,'#') from user
select name,left(name,1) from user
select name,right(name,1) from user
select ename,substring(ename,2,2) from user
select ename,substring(ename,2) from user
select trim(' java ')
select ename,replace(ename,'x','a') from user
select name,repeat(name,2) from user
select name,reverse(name) from user
select ename,insert(ename,2,2,'txc')from user
select name,insert(name,2,0,'-') from user
select ename,lower(ename) from user
select ename,upper(ename) from user
5.数字函数
select money,abs(money) from user
select money,ceil(money) from user
select money,floor(money) from user
select money,round(money/100),round(money/100,1),round(money,-1) from user
select money,truncate(money/100,1),truncate(money,-1) from card
select money,money*rand() from user
select money,mod(money,2)from user
6.日期函数
select now();
select sysdate()
-- 年月日时分秒
select date_format(openDate,'%Y年%m月%d日 %H:%i:%s') from card
-- 时分秒
select time_format(transDate,'%H:%i:%s') from transinfo
insert into card(cardNo,openDate,money,password)
values('011', str_to_date('05/20/2019', '%m/%d/%Y'),1000,'123456')
select openDate,date_add(openDate,interval 1 day) from card
-- 结果是得到的是天数
select openDate, datediff(now(),openDate) from card
select transDate,year(transDate),month(transDate),day(transDate) ,hour(transDate),minute(transDate),second(transDate) from transinfo
select transDate,dayOfWeek(transDate),weekDay(transDate) from transinfo
select LAST_DAY(opendate) from card
7.多行函数
select sum(age) from user
select avg(age) from user
select count(userId) from user
select max(age) from user
select min(age) from user
select group_concat(name) from user