MySQL选择具有多个条件的查询

我已经写了一个MySQL查询,但似乎没有工作,因为结果是空的。我的代码有错吗?

$result = mysql_query(
     "SELECT user_id 
      FROM wp_usermeta 
      WHERE 
         (meta_key = 'first_name' AND meta_value = '$us_name') AND         
         (meta_key = 'yearofpassing' AND meta_value = '$us_yearselect') AND 
         (meta_key = 'u_city' AND meta_value = '$us_reg') AND 
         (meta_key = 'us_course' AND meta_value = '$us_course')"
);
我会用这个查询:

SELECT
  user_id
FROM
  wp_usermeta 
WHERE 
  (meta_key = 'first_name' AND meta_value = '$us_name') OR 
  (meta_key = 'yearofpassing' AND meta_value = '$us_yearselect') OR 
  (meta_key = 'u_city' AND meta_value = '$us_reg') OR
  (meta_key = 'us_course' AND meta_value = '$us_course')
GROUP BY
  user_id
HAVING
  COUNT(DISTINCT meta_key)=4

这将选择满足所有四个条件的所有user_id。

http://stackoverflow.com/questions/16249731/mysql-select-query-with-multiple-conditions

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:MySQL选择具有多个条件的查询