gpt4 book ai didi

mysql - SQL 查询以区分使用大小写的行

转载 作者:行者123 更新时间:2023-11-29 01:54:43 30 4
gpt4 key购买 nike

这是我正在查询的表中的数据的样子

表一

ITEM     SEQUENCE     CODE
Item1 1 A
Item1 2 B
Item2 1 B
Item2 2 C
Item2 3 D

我当前的查询看起来像

  Select Distinct Table1.ITEM, 
case when Table1.SEQUENCE = '1' Then Table2.DSC end As FirstDSC,
Case When Table1.SEQUENCE = '2' then Table2.DSC End As SecondDSC,
Case When Table1.SEQUENCE = '3' Then Table2.DSC End As ThirdDSC
From Table1
Join Table2 on Table2.Code = Table1.Code
Where Table1.Item In (Subquery here to find distinct values that Item can be)

它目前返回的数据看起来像

ITEM  FIRSTDSC SECONDDSC THIRDDSC
Item1 DSC-A NULL NULL
Item1 NULL DSC-B NULL

我想知道如何让数据返回看起来像

ITEM FIRSTDSC  SECONDDSC  THIRDDSC
Item1 DSC-A DSC-B NULL
Item2 DSC-B DSC-C DSC-D

有没有好的方法可以做到这一点,还是我目前的查询方向完全错误?

最佳答案

我认为您需要group by 和条件聚合,而不是distinct:

Select Table1.ITEM, 
max(case when Table1.SEQUENCE = '1' Then Table2.DSC end) As FirstDSC,
max(Case When Table1.SEQUENCE = '2' then Table2.DSC End) As SecondDSC,
max(Case When Table1.SEQUENCE = '3' Then Table2.DSC End) As ThirdDSC
From Table1 Join
Table2
on Table2.Code = Table1.Code
Where Table1.Item In (Subquery here to find distinct values that Item can be)
group by table1.ITEM;

关于mysql - SQL 查询以区分使用大小写的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32320018/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com