SQL重点语句

1、A、B两表有相同字段,根据两表相同字段更改A中某字段的值为B中相应字段的值。


SQL语句为如下中的一种:

update A set A.bonus=B.bonus from A inner join B on B.deptid=A.deptid;  
update A set A.bonus=(select B.bonus from A inner join B on B.deptid=A.deptid);  
update A set A.bonus="B.bonus from A inner join B on B.deptid=A.deptid";

2、找出E表中工资高于所在部门的平均工资的员工。


SQL语句为:

SELECT * FROM tb_staff 
WHERE salary>(SELECT AVG(salary) FROM tb_staff GROUP BY dept_id);

3、在关联的三张表中取出需求字段
三张表分别为商品tb_item、商品分类tb_item_cat、商品详情tb_item_desc。

tb_item表字段有(id、title、sell_point、price、num、barcode、image、cid、status、created、updated)

tb_item_cat表字段有(id、parent_id、name、status、sort_order、is_parent、created、updated)

tb_item_desc表字段有(item_id、item_desc、created、updated)

SQL语句为:

SELECT 
a.id,
a.title,
a.sell_point,
a.price,
a.image,
b.name catagory_name,
c.item_desc
FROM tb_item a LEFTJOIN tb_item_cat b on a.cid=b.id
LEFTJOIN tb_item_desc c on a.id=c.item_id
WHERE a.status=1;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容