UNION和OR谓词

找出 product 和 product2 中售价高于 500 的商品的基本信息.

select  *
from product
where purchase_price>500

union

select *
from product2
where purchase_price>500;

 

交运算INTERSECT

该运算在Mysql8.0中不支持

差运算EXCEPT 运算

该运算在Mysql8.0中也不支持,不过我们可以采用NOT IN来代替

连结 (JOIN)

是SQL中一种添加列的运算

内连结

-- 内连结
FROM <tb_1> INNER JOIN <tb_2> ON <condition(s)>

这里要注意的主要是内连结合where一起使用的情况。

这里可以这么写

SELECT  SP.shop_id
       ,SP.shop_name
       ,SP.product_id
       ,P.product_name
       ,P.product_type
       ,P.sale_price
       ,SP.quantity
  FROM shopproduct AS SP
 INNER JOIN product AS P
    ON SP.product_id = P.product_id
 WHERE SP.shop_name = '东京'
   AND P.product_type = '衣服' ;

这时候查询的执行顺序就是:FROM 子句->WHERE 子句->SELECT 子句

 

自连结(SELF JOIN)

就是一张表与自身做连接

 

外连结(OUTER JOIN)

内连结会丢弃两张表中不满足 ON 条件的行,和内连结相对的就是外连结. 外连结会根据外连结的种类有选择地保留无法匹配到的行.

按照保留的行位于哪张表,外连结有三种形式: 左连结, 右连结和全外连结.

 

posted on 2020-12-22 22:35  何莫道  阅读(117)  评论(0编辑  收藏  举报