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
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";

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.