$data = $con->query("SELECT id, content FROM table1 WHERE id IN (". $column['data'] .")");
我上面有需要添加if then语句的代码。
if table1.draftid = 0, then select content from table 1
if table1.draftid != 0, then select content from table 2
仅供参考...。
如果TABLE1.DRAFTID!= 0,然后
table1.draftid = table2.id
我尝试了以下方法。
$data = $con->query("SELECT table1.id,
(case
when table1.draftid = 0 then table1.content
when table1.draftid != 0 then (select table2.content from table2 where table1.draftid = table2.id)
end) as data,
FROM table1
JOIN table2 ON table1.draftid = table2.id
WHERE table1.id IN (". $column['data'] .")");
使用例
> Table 1
>
>id = 1
>
>draftid = 1
>
>content = Table 1 Test
---------
> Table 2
>
> id = 1
>
> content = Table 2 Content
所需的结果:
If table 1 draftid = 0
,则它将调用表1中的内容。但是,如果
table1.draftid != 0
(例如:1),则>它将从表2中的内容中提取table1.draftid = table2.id
最佳答案
你可以试试这个吗?
$data = $con->query("SELECT table1.id,
(case
when table1.draftid = 0 then table1.content
when table1.draftid != 0 then table2.content
end) as data,
FROM table1
JOIN table2 ON table1.draftid = table2.id
WHERE table1.id IN (". $column['data'] .")");
关于mysql - 如果/然后陈述,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35409195/