简体   繁体   中英

If/Then Statement

$data = $con->query("SELECT id, content FROM table1 WHERE id IN (". $column['data'] .")");

I've got the code above that I need to add an if then statement to.

if table1.draftid = 0, then select content from table 1
if table1.draftid != 0, then select content from table 2

Just for reference.... IF TABLE1.DRAFTID != 0, then table1.draftid = table2.id

I tried the following..

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

Example of use

> Table 1
>
>id = 1
>
>draftid = 1
>
>content = Table 1 Test

---------

> Table 2
>
> id = 1
>
> content = Table 2 Content

The desired results: If table 1 draftid = 0 , then it would call the content from table 1. BUT, if table1.draftid != 0 (In example: 1), >then it would pull the content from table 2 where table1.draftid = table2.id

Can you try this?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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