SQL查询和判断语句

  1. --数据库多表的连接查询  
  2. 一、外连接  
  3. 1.左连接 left join / left outer join  
  4. 左外连接包含left join 左表里的所有行,若左表在右表没有匹配,则结果中对应  
  5. 行的右表部分全部为空  
  6. select * from student left join course on student.ID =course.ID   
  7.   
  8. 2. 右连接 right joinright outer join   
  9. 3. 完全外连接   full join 或 full outer  join   
  10. select * from student full join course on student.ID =course.ID   
  11.   
  12. 二、内连接 join / inner join   
  13. inner join 是比较运算符,返回符合条件的行  
  14. select * from student,cour  where student.ID=course.ID;  
  15.   
  16. 三、交叉连接 cross join  
  17. 第一个表的行数乘以第二个表的行数等于笛卡尔积结果集的大小  
  18. select * from student cross join course  
  19. 若在此sql上加上条件where 语句例如   
  20. select * from student cross join course where student.ID  =course.ID   
  21. 则结果和内连接一样  
  22. 四、当两表关系为一对多,多对一,或多对多时  
  23. 例如学生、课程、学生-选课情况,一个学生可以选择多个课程,一个课程也可以被多个学生  
  24. 选择  
  25. 此时student和course就是多对多的关系  
  26. 当满足多对多关系时,需要一个中间表如S-C  
  27. select s.Name, c.Name from   
  28. S_C as sc left join Student as s on s.sno=sc.sno   
  29. left join Course as c on c.cno=sc.cno  
  30.   
  31. --mysql 查询的五种子句  
  32. 1.where 常用运算符  
  33. 1.1比较运算符:  
  34. 大于>  小于 <     等于=   不等于!= 或<>   
  35.  大于等于>=  小于等于<=  
  36.  in (v1,v2……vn)   
  37.  between v1 and v2 左右都是闭区间  
  38.  1.2逻辑运算符:  
  39.  逻辑非 not(!)   
  40.  逻辑或 or(||)  
  41.  逻辑与 and(&&)  
  42.  例如:  
  43.  where price>=3000 and price<=5000 or price >=500 and pric <=1000  
  44.  或  
  45.  where price not between 3000 and 5000  
  46.  1.3 模糊查询  
  47.  像 like  
  48.  通配符: %任意字符 _单个字符  
  49.  例如where goods_name like  'iphone%';  
  50.  where goods_name like '诺基亚N_';  
  51.  2. group by 分组, 一般情况下group by 与统计函数一起使用才有意义  
  52.  例如:  
  53.  select goods_id,good_name,cat_id,max(shop_price)   
  54.  from goods   
  55.  group by cat_id;  
  56.  这里因为没有用聚合函数,所以查询语句是有问题的  
  57.  MySQL的五种统计函数  
  58.  2.1 max 求最大值  
  59.  select max(good_price) from goods group by cat_id-- 取出每个cat里最大的一个值  
  60.  select good_id, max(good_price) from goods group by good_id;--查询价格最高的商品编号  
  61.    
  62.  2.2 min 求最小值  
  63.  2.3 sum 求总数和  
  64.  select sum(goods_number) from goods;  
  65.  2.4 avg 求平均值  
  66.  select cat_id, avg(good_price) from goods group by  cat_id;  
  67.  2.5 count 求总行数  
  68.  select cat_id,count(*) from goods group by cat_id;  
  69.  --将每个字段名当做变量来理解,可以进行计算  
  70.  --1.查询本店每个商品比市场价低多少  
  71.  select goods_id,good_name, good_price - market_price from goods;  
  72.  --2.查询本店每个栏目下面积压的货款  
  73.  select cat_id,sum(goods_price * goods_number) from goods group by cat_id;  
  74.  -- 可用as 来给计算结果取别名  
  75.  select cat_id,sum(good_price*good_number) as hk from goods group by cat_id;  
  76.    
  77.  3. having 和where的异同点  
  78.  havingwhere类似,可用来筛选数据,where后的表达式怎么写,having后就怎么写  
  79.  但是   
  80.  where 针对表中的列发挥作用,查询数据  
  81.  having 针对查询结果中的列发挥作用,筛选数据  
  82.  --查询本店商品价格比市场价低多少钱,输出低200元以上的商品,  
  83.  --这里不能用where因为s 是查询结果,where只能对表中的字段进行筛选  
  84.  select good_id ,good_name, market_price-shop_price as s   
  85.  from goods   
  86.  having s>200;  
  87.  --如果要用where的话应该  
  88.  select good_id,good_name from goods  
  89.  where market_price-shop_price>200;  
  90.  --如果想要同时使用where和having应该怎么做?  
  91.  select cat_id,goods_name,market_price - shop_price as s from goods where cat_id = 3 having s > 200;  
  92.  --查询货款总和超过2000 的栏目,以及该栏目的积压货款  
  93.  select cat_id, sum(shop_price*good_number) as t   
  94.  from goods  
  95.  group by cat_id  
  96.  having s>2000  
  97.    
  98.  --查询两门及两门以上科目不及格的学生的平均分  
  99.  (1)先查询 科目不及格的学生名及分数  
  100.  select name,score<60 from student;  
  101.  (2) 查询两门及两门以上科目不及格 的学生名及不及格科目  
  102.  select namesum(score<60) as bujige from student   
  103.  group by name having bujige >1;  
  104.  (3) 查询两门及两门以上科目不及格的学生的平均分  
  105.  select nameavg(score)as pingjunfen, sum(score<60) as bujige  
  106.  from stu  group by name  
  107.  having bujige>1;  
  108.    
  109.  4.order by  
  110.  4.1 order by price //默认升序排列  
  111.  4.2 order by pric desc //降序排列  
  112.  4.3 order by price asc //升序  
  113.  4.4 order by rand() //随机排列,效率不高  
  114.    
  115.  --按cat_id 升序排列,按每个cat_id下商品价格降序排列  
  116.  select * from goods where cat_id!=2 order by cat_id,price desc  
  117.    
  118.  5.limit   
  119.  limit [offset], N  
  120.  offset 偏移量,可选,不写则相当于 limit 0,N  
  121.  N 是取出条目  
  122.  --取出价格第四-六高的商品(3,3)  
  123.  select good_id,good_name,good_price from order by good_price desc limit 3,3;  
  124.    
  125.  --查询每个cat_id 下面最贵的商品  
  126.  --先对每个cat_id下的商品排序,将此查询结果放在临时表中  
  127.  select cat_id, good_id, good_name, shop_price   
  128.  from goods order by cat_id, shop_price desc;  
  129.  --再从临时表中选出最贵的商品  
  130.  select * form   
  131.  (  
  132.   select cat_id, good_id, good_name, shop_price   
  133.  from goods order by cat_id, shop_price desc  
  134.  ) as t  group by cat_id;--因为group by 前没有使用聚合函数,所以选取第一行  
  135.    
  136.  ----------------------------------------------------------------  
  137.  --MySQL的子查询  
  138.  --where型子查询  
  139.  select goods_id,goods_name from goods where goods_id =  
  140.  (select max(goods_id) from goods);--取出最新产品  
  141.    
  142.  select cat_id,good_id,good_name,from goods where goods_id in   
  143.  (  
  144.  select max(goods_id) from goods group by cat_id);  
  145.  )  
  146.    
  147.  --from 型子查询  
  148.  select name,avg(score) from student  
  149.  where name in   
  150.  (select name from   
  151.  (select namecount(*) as bujige from student having  
  152.  bujige >=2 and score<60)   
  153.  )  
  154.  group by name;  
  155.    
  156.  --exists 型子查询  
  157.  select cat_id,cat_name from catgory where   
  158.  exists  
  159.  (select * from goods where goods.cat_id=catgory.cat_id);  
  160.  --查询哪些栏目下有商品  
  161.    
  162.  --union 的用法  
  163.    (把两次或多次的查询结果合并起来,要求查询的列数一致,推荐查询的对应的列类型一致,可以查询多张表,多次查询语句时如果列名不一样,则取第一次的列名!如果不同的语句中取出的行的每个列的值都一样,那么结果将自动会去重复,如果不想去重复则要加all来声明,即union all)  
  164.            ## 现有表a如下  
  165.                 id  num  
  166.                 a    5  
  167.                 b    10  
  168.                 c    15  
  169.                 d    10  
  170.             表b如下  
  171.                 id  num  
  172.                 b    5  
  173.                 c    10  
  174.                 d    20  
  175.                 e    99  
  176.             求两个表中id相同的和  
  177.            select id,sum(num) from (select * from ta union select * from tb) as tmp group by id;  
  178.             //以上查询结果在本例中的确能正确输出结果,但是,如果把tb中的b的值改为10以查询结果的b的值就是10了,因为ta中的b也是10,所以union后会被过滤掉一个重复的结果,这时就要用union all  
  179.             select id,sum(num) from (select * from ta union all select * from tb) as tmp group by id;  
  180.                   
  181.             #取第4、5栏目的商品,按栏目升序排列,每个栏目的商品价格降序排列,用union完成  
  182.             select goods_id,goods_name,cat_id,shop_price from goods where cat_id=4 union select goods_id,goods_name,cat_id,shop_price from goods where cat_id=5 order by cat_id,shop_price desc;  
  183.             【如果子句中有order by 需要用( ) 包起来,但是推荐在最后使用order by,即对最终合并后的结果来排序】  
  184.             #取第3、4个栏目,每个栏目价格最高的前3个商品,结果按价格降序排列  
  185.              (select goods_id,goods_name,cat_id,shop_price from goods where cat_id=3 order by shop_price desc limit 3) union  (select goods_id,goods_name,cat_id,shop_price from goods where cat_id=4 order by shop_price desc limit 3) order by shop_price desc;  
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值