gpt4 book ai didi

java - 是否可以使用 if else 条件 true 从一个表中获取另一个表?

转载 作者:太空宇宙 更新时间:2023-11-03 10:36:43 25 4
gpt4 key购买 nike

select * from table1 t1 if( t1.purchase =='1')
select t2.price from table2 t2 where t2.id=t1.id
else
select t3.price from table3 t3 where t3.id=t1.id
end if as amt

我尝试了这个但是它不起作用如何在 SQL 中使用 if 条件并从 MySQL 数据库中获取详细信息请给提示伙计们。

我问的是表 2 的条件是否为真,否则表 3 不是同一个问题

最佳答案

不知道您的表格、数据和预期输出。
问题应该包括这些以获得好的答案。
但是这个查询可能会成功。

SELECT
*
, IF(t1.purchase = '1', t2.price, t3.price) as amt
FROM
table1 t1

INNER JOIN
table2 t2
ON
t1.id = t2.id

INNER JOIN
table3 t3
ON
t1.id = t3.id

或者在 table2 或 table3 中缺少值时使用 LEFT JOIN(因为关闭了 Turo 的评论而编辑)

SELECT
*
, IF(t1.purchase = '1', t2.price, t3.price) as amt
FROM
table1 t1

LEFT JOIN
table2 t2
ON
t1.id = t2.id

LEFT JOIN
table3 t3
ON
t1.id = t3.i

关于java - 是否可以使用 if else 条件 true 从一个表中获取另一个表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47074530/

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