SQL重点语句

2017-05-19  本文已影响0人  黑小胡子

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;
上一篇 下一篇

猜你喜欢

热点阅读