Jump to content

SELECT query help for IF clauses


mavera2

Recommended Posts

I'm trying to write a query for my MySQL database.

I can successfully query the database but something i couldn't did.

 

$firstcriteria='name';
$secondcriteria='age';
$ageMax='30';

$query =  "SELECT *  FROM mytable
WHERE '$firstcriteria' IS NOT NULL
AND IF  ( '$firstcriteria' = 'mark', 1,  ( IF ('$secondcriteria' < '$ageMax', 1, 0)   )   )
AND ORDER BY '$secondcriteria' ASC;

 

Where name is not null part: OK

and if part: NOT OK

I think there is an error of syntax etc.

 

What would be the syntax when we use IF clause in sql queries for MySQL?

 

Thank you

 

Link to comment
https://forums.phpfreaks.com/topic/251000-select-query-help-for-if-clauses/
Share on other sites

Thank you Pikachu for help.

 

There shouldn't be an AND before the ORDER BY clause, that much is certain. What error is MySQL returning when you execute the query?

 

 

No error appears because sql gives results, but false results.

 

query1 gives right results,

but the response of the query2 gives results which includes NULL and NOT NULL rows of firstcriteria

 

$firstcriteria='name';
$query1 =  "SELECT *  FROM mytable WHERE name IS NOT NULL";
$query2 =  "SELECT *  FROM mytable WHERE '$firstcriteria' IS NOT NULL";

 

 

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.