请有人建议如何根据表 A 和表 B 信息为下面的结果编写 mySQL 查询。
Table A
=======
ID | Product
------------
1 | A
2 | B
3 | C
Table B
=======
ID | Link | Extra Field | Extra Value
-------------------------------------
1 | 1 | Tax | Yes
2 | 1 | Photo | No
3 | 2 | Tax | Yes
4 | 2 | Photo | Yes
5 | 3 | Tax | No
6 | 3 | Photo | Yes
Result
======
Product | Tax | Photo
---------------------
A | Yes | No
B | Yes | Yes
C | No | Yes
最佳答案
一种方法是使用条件聚合的join
和group by
查询。
select a.product,
max(case when field1 = 'tax' then field2 end) as tax,
max(case when field1 = 'photo' then field2 end) as photo
from a join
b
on a.id = b.link
group by a.product;
关于MySQL 数据库连接查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29417453/