gpt4 book ai didi

mysql - 条件sql查询

转载 作者:行者123 更新时间:2023-11-29 04:28:04 25 4
gpt4 key购买 nike

我有 2 个表,根据第一个表的值,我必须在第二个表上选择一列。

例子

表1

data1|data2|type
P3 | C | R
P1 | D | N
P2 | C | R
P1 | C | N
P2 | D | R
P3 | D | N

表2

name|revolution|nibs
C |repeat |may
D |genocide |fill

所以,我想根据这样的类型获取 data1 的值(R 代表旋转,N 代表 Nib )

查询 P3:

name|value
C |repeat
D |fill

查询 P1

name|value
C |may
D |fill

谢谢

最佳答案

对此的标准方法是使用 CASE:

select 
t1.data2,
case t1.type
when 'R' then t2.revolution
when 'N' then t2.nibs
end
from table1 t1
join table2 t2 on t1.data2 = t2.name
where data1 = ?

关于mysql - 条件sql查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7439659/

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