在WHERE语句中使用相同的列名称,使用“AND”而不是“OR”

时间:2014-03-13 03:53:06

标签: php mysql

我正在尝试使用implodes成为一个where语句,并且无论出于什么原因我都无法做到。 b.id_value有多个值,我必须利用这些值才能返回结果。

他们需要明确而不是声明。有任何想法吗?它是从mysql拉出来的,我正在使用php。我阻止引用我认为我遇到问题的部分。提前谢谢

$strSQL = "SELECT DISTINCT a.id, uu.upload_path, b.id_user, UNIX_TIMESTAMP(a.date_registration) as date_registration, e.id_user as session 
FROM ".USERS_TABLE." a 
JOIN resdescr_spr_user b ON a.id=b.id_user
LEFT JOIN ".USER_UPLOAD_TABLE." uu ON uu.id=a.icon_path 
LEFT JOIN ".ACTIVE_SESSIONS_TABLE." e ON a.id=e.id_user 
WHERE 
    a.id IN (".$in_str.") 
    AND (a.id_city IN (".$excludes.") OR a.id_city='$zipcode') 
    AND a.id IN (".implode(', ', $ids).") 
    AND (b.id_value IN (".implode(', ', $idf).") AND b.id_value IN (".implode(', ', $idc).") AND b.id_value IN (".implode(', ', $idd)."))
    AND NOT a.id='$enemy'";

$rs = $dbconn->Execute($strSQL);

1 个答案:

答案 0 :(得分:0)

我认为你误解了AND条件的IN的含义。您正确使用括号表示这3个条件。但你可能应该使用OR

$strSQL = "SELECT DISTINCT a.id, uu.upload_path, b.id_user, UNIX_TIMESTAMP(a.date_registration) as date_registration, e.id_user as session 
FROM ".USERS_TABLE." a 
JOIN resdescr_spr_user b ON a.id=b.id_user
LEFT JOIN ".USER_UPLOAD_TABLE." uu ON uu.id=a.icon_path 
LEFT JOIN ".ACTIVE_SESSIONS_TABLE." e ON a.id=e.id_user 
WHERE 
    a.id IN (".$in_str.") 
    AND (a.id_city IN (".$excludes.") OR a.id_city='$zipcode') 
    AND a.id IN (".implode(', ', $ids).") 
    AND (b.id_value IN (".implode(', ', $idf).") OR b.id_value IN (".implode(', ', $idc).") OR b.id_value IN (".implode(', ', $idd)."))
    AND a.id != '$enemy'";

$rs = $dbconn->Execute($strSQL);