我需要以下任务的帮助:
Table_A包含列(ID,名称);
Table_B包含列(ID,类别)。

有两个可能的类别(Category_A,Category_B)。

我想要以下格式的结果表:

|Name|Category_A|Category_B|
----------------------------
|Lilu|    NO    |   YES    |


但是Table_B可以针对同一ID包含两行,例如“ Lilu”也属于Catogory_A和Category_B。但是结果是我需要输入以下内容:

|Name|Category_A|Category_B|
----------------------------
|Lilu|   YES    |   YES    |


我准备查询,但我错过了分组...

    select A.ID, --B.Category
case when B.category = 'Category_A' then 'yes' else 'no' end Category_A,
case when B.category = 'Category_B' then 'yes' else 'no' end Category_B
from system.A_table A, B_table B;


我该如何实施?

最佳答案

尝试使用

select A.name,
case when exist (Select b.Id from B_table b where b.id=A.id and b.category = 'Category_A') then 'yes' else 'no' end Category_A,
case when exist (Select b.Id from B_table b where b.id=A.id and b.category = 'Category_B') then 'yes' else 'no' end Category_B
from system.A_table A

关于mysql - SQL案例几行分析,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39755874/

10-11 03:09
查看更多