php - MySQL Query - WHERE and IF? -
solved: datatypes in table country , os set varchar in where, changed = 0 = '0'. raina77ow pointing out! appreciate it.
i'm not quite sure how write query. basically, i'm going have table 2 columns (os , country_code) - more columns too, conditional ones. these either set 0 all, or specific ones, separated commas.
now, i'm trying achieve pull data table if os , country_code = 0, or if contain matching data (separated commas).
then, have column time. want select rows time greater time column, unless column time_t set false, in case shouldn't matter.
i hope explained right?
this kind of have far:
$get = $db->prepare("select * commands country_code = 0 or country_code :country_code , os = 0 or os :os , if (time_t = 1, expiry > now()) "); $get->execute(array( ':country_code' => "%{$data['country_code']}%", ':os' => "%{$data['os']}%" ));
edit: code i'm using now, still can't seem os , country_code part working:
$get = $db->prepare("select * commands (country_code = 0 or country_code :country_code) , (os = 0 or os :os) , (time_t <> 1 or expiry > now()) "); $get->execute(array( ':country_code' => "%{$data['country_code']}%", ':os' => "%{$data['os']}%" ));
data sent db be: os => windows 7 country_code => gb 1 being sent, not more one. issue i'm having right is, it's working os , time thing, it's not working country. tried raw query, when data in table "fr, sa" , did '%gb%' still returned row
it's question of logical operators' precedence: and
higher or
in ladder, original query executed as...
where country_code = 0 or (country_code ... , os = 0) or os like...
use parenthesis separate conditions, this:
where (country_code = 0 or country_code ...) , ( os = 0 or os ...)
and final filter condition in query, suppose, should written as...
and ( time_t <> 1 or expiry > now() )
update: ... , it's should country_code = '0'
or in code. you're checking varchar column, remember.
Comments
Post a Comment