sql语句需要加分号吗_产品经理需要了解的SQL语句

举例表结构

4d5bead13f807449349a04583dd2647d.png

t_user表

2009cbbe1fc1fd5441ca940e518b5e6e.png

t_book表

5dca742155fdee644ac78f60191eb1df.png

t_cartitem表

b6d521c7127e9b862b5537dce98b2a0f.png

t_category表

bb5c897b2404d84eb377c607a34552b7.png

基础操作

  1. 增:insert into t_user VALUES(5,"xiaoming",12345,"254@qq.com",0,"xxxx",null,null)
  2. 删:delete from t_user where uid=5;
  3. 改:update t_user set loginname="ZS" where uid=6;
  4. 查:select * from 要查询的表名 where 条件,例如

06ee070893573bc016fb4d10792921e1.png

单表条件查询

  1. 取消重复行DISTINCT:select DISTINCT loginname from loginpass=1234
  2. 查询满足条件的元组:
    1. 比较大小:select bname from t_book where price<60
    2. 确定范围:select bname from t_book where price between 20 and 50
    3. 确定集合:select bname from t_book where press in ('清华大学出版社','人民邮电出版社')
    4. 字符匹配:[not] like '匹配串'
      1. %代表任意长度,a%b代表以a开头以b结尾的任意字符串,如acb、addfgdgb,例如select * from t_user where loginname like 'z%'
      2. _代表任意单个字符,a_b代表以a开头以b结尾的长度为3的任意字符串,如atb,例如select * from t_user where loginname like 'a_c'
    5. 涉及空值的查询只能用is不能用“=”:select * from t_user where gender is NULL
  3. 排序order by默认升序:select bname from t_book where authur='霍斯特曼' order by price
  4. 聚集函数:select 聚集函数 from 表名 where 条件,聚集函数不能用在条件里
    1. 统计元组个数:count(*)
    2. 计算某一列值的总和:sum(列名)
    3. 计算某一列值的平均值:avg(列名)
    4. 求某一列最大值:max(列名)
    5. 求某一列最小值:min(列名)
  5. 查询结果分组group by:以作者分组并查找该作者书籍均价大于50的作者,select author, avg(price) from t_book group by author having avg(price)>50

多表查询

  1. 连接查询
    1. 等值与非等值连接:例如查询在购物车中的书籍信息 select t_book.* from t_book,t_cartitem where t_book.bid=t_cartitem.bid
    2. 外连接:取left左边的表的元组数,查找购物车中的书籍名和加购数量 select t_book.bname,t_cartitem.quantity from t_cartitem left outer join t_book on (t_book.bid=t_cartitem.bid)
    3. 多表连接:查询购物车中的用户名和书名 select t_user .loginname,t_book.bname from t_book,t_cartitem,t_user where t_book.bid=t_cartitem.bid and t_user.uid= t_cartitem.uid
  2. 嵌套查询
    1. 带有in谓词的子查询:查询类别为程序设计的图书名 select bname from t_book where cid in (select cid from t_category where cname='程序设计')
    2. 带有比较运算符的子查询:
      1. 查询类别为程序设计的图书名 select bname from t_book where cid = (select cid from t_category where cname='程序设计');
      2. 查询大于所有书籍平均价格的书 select bname,price from t_book x where price >= (select avg(price) from t_book y where x.bname=y.bname)
    3. 带有any或all谓词的子查询
      1. 查询程序设计类书籍比任意一本数据库书籍价格低的书籍名称:select bname price from t_book where price < any(select price from t_book where cid=1) and cid=5
      2. 查询程序设计类书籍比所有数据库书籍价格都低的书籍名称:select bname price from t_book where price <all(select price from t_book where cid=1) and cid=5
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值