Jump to content

IF statement in SELECT QUERY


Canman2005

Recommended Posts

Hi all

 

I wonder if someone can help.

 

Is it possible to add an "if" statement into a QUERY?

 

For example, I have

 

SELECT p.id, p.name, p.topic FROM people p WHERE p.type = 'Math';

 

But I want to add

 

if(p.topic == 'today') { p.date }

 

to the end of

 

SELECT p.id, p.name, p.topic

 

so it would look something like

 

SELECT p.id, p.name, p.topic, if(p.topic == 'today') { p.date } FROM people p WHERE p.type = 'Math';

 

 

So in real terms it would end up looking like either

 

SELECT p.id, p.name, p.topic FROM people p WHERE p.type = 'Math';

 

or if

 

topic equaled "today"

 

then it would use

 

SELECT p.id, p.name, p.topic,p.date FROM people p WHERE p.type = 'Math';

 

I know the above is not possible, but what would I change it to if at all possible.

 

Thanks all

 

Ed

Link to comment
https://forums.phpfreaks.com/topic/205660-if-statement-in-select-query/
Share on other sites

thanks, that seemed to work great

 

Just another question, but is it also possible to add something like that at the end of the QUERY, so adding

 

JOIN t.things ON t.userid = p.id

 

to the end of

 

SELECT p.id, p.name, p.topic FROM people p WHERE p.type = 'Math';

 

so I guess it would be something like

 

SELECT p.id, p.name, p.topic FROM people p (p.topic = 'yesterday', JOIN t.things ON t.userid = p.id) WHERE p.type = 'Math';

 

Thanks very much

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.