查询
语法:关键字:order by 字段名 (默认排序是升序)
asc升序/desc降序
单条件查询
select name from t_user where name=‘java’ order by name asc
多条件查询
select name from t_user where name=‘java’ and age=‘20’ order by name asc
按照升序显示name字段信息
模糊查找(fuzzy lookup)
关键字:LIKE
LIKE ‘L%’
LIKE’%L’
LIKE’%L%’
LIKE’L%L%L’ #匹配字段中的内容
select id,name,age from t_age where name LIKE ‘%清’
企业常用的场景:模糊查询订单、姓名、地址、商品名称、商品分类
前端不允许输入%,前端过来的值是放在’'号中间。
范围查询
mysql:login_date<‘2020-03-12’; oracle:login_date <to_char(‘2021-03-12’)>
to_char()函数,专门处理数据格式
login_date BETWEEN ‘2021-03-12’ and ‘2021-04-01’ 某某值在一个区间中
这个写法可以换成:login_date >='2021-03-12 'and login_date <= ‘2021-04-01’
运算符
1)算术运算符
2)比较运算符
=、<、>、>=、<=、<=>、<>或!=
IS NULLl或ISNULL、IS NOT NULL、LEAST、GREATEST、BETWEEN AND、IN、
NOT IN、LIKE、REGEXP
列如
select * from t_user where id in ('1','2') and name is not null ;
3)位运算符
4)逻辑运算符
内联查询
语法格式:
select e.age,t.name from t_age e,t_user t where e.id=t.id
select name,age from t_age a inner JOIN t_user t ON t.id=a.id
select age,t.name from t_age a inner JOIN t_user t ON t.id=a.id
inner JOIN on 会将t_age 最为主表和t_user进行关联查询,当t_age表中没有数据存在时,它是查询不出来的,就算t_user有值。
外联查询
select name,age from t_user a left outer join t_age d ON a.uname=d.name