Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何在SQL中拆分带条件的表并与其他表联接_Mysql_Sql - Fatal编程技术网

Mysql 如何在SQL中拆分带条件的表并与其他表联接

Mysql 如何在SQL中拆分带条件的表并与其他表联接,mysql,sql,Mysql,Sql,我有两张桌子: 销售: 工作人员: 如何按AM/PM类型拆分Sales表并与Staff表联接 结果应该如下所示: 您可以使用不同的别名a和b两次加入表sales 例如: select s.id, s.name, a.value as am b.value as pm from staff s left join sales a on a.id = s.id and a.type = 'AM' left join sales b on b.id = s.id and b.typ

我有两张桌子:

销售:

工作人员:

如何按AM/PM类型拆分Sales表并与Staff表联接

结果应该如下所示:


您可以使用不同的别名
a
b
两次加入表
sales

例如:

select
  s.id,
  s.name,
  a.value as am
  b.value as pm
from staff s
left join sales a on a.id = s.id and a.type = 'AM'
left join sales b on b.id = s.id and b.type = 'PM'

首先,需要使用union向上旋转此表,然后应用联接,如下所示-



谢谢

Select Name,A.ID,AM,PM From
(Select ID,Value as AM, ' ' from Sales where type='AM' 
 union 
 Select ID, ' ' , Value as PM from Sales where type='PM')A
Inner Join
(select ID, Name from Staff)B ON A.ID=b.ID