繁体   English   中英

If / Then陈述

[英]If/Then Statement

$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'] .")");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM