MySQL Query - WHERE and IF?
- by Prash
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']}%"
));