簡體   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