MySQL Query - WHERE and IF?

Posted by Prash on Stack Overflow See other posts from Stack Overflow or by Prash
Published on 2012-07-07T15:06:18Z Indexed on 2012/07/07 15:15 UTC
Read the original article Hit count: 142

Filed under:
|
|
|

I'm not quite sure how to right this query. Basically, I'm going to have a table with two columns (OS and country_code) - more columns too, but those are the conditional ones. These will be either set to 0 for all, or specific ones, separated by commas.

Now, what I'm trying achieve is pull data from the table if the OS and country_code = 0, or if they contain matching data (separated by commas).

Then, I have a column for time. I want to select rows where the time is GREATER than the time column, unless the column time_t is set to false, in which case this shouldn't matter.

I hope I explained it right?

This is what I kind of have so far:

$get = $db->prepare("SELECT * FROM commands 
    WHERE country_code = 0 OR country_code LIKE :country_code 
    AND OS = 0 OR OS LIKE :OS 
    AND IF (time_t = 1, expiry > NOW())
");
$get->execute(array(
    ':country_code' => "%{$data['country_code']}%",
    ':OS' =>  "%{$data['OS']}%"
));

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql