MySQL多个内部联接与Where子句

时间:2014-01-29 15:34:03

标签: mysql sql relational-database

我对mysql有点问题。查询如下:

SELECT u.user_id,u.gender, ua.age,u.name,u.isonline,dot.profile_image_no FROM user_table AS u
INNER JOIN user_answers_table AS ua ON u.user_id = ua.user_id
INNER JOIN data_one_table AS dot ON dot.user_id = u.user_id
INNER JOIN user_detail_table AS udt ON udt.user_id = u.user_id
WHERE u.isregistrationcomplete='1' AND u.isactive='1' AND u.gender = '0' AND ua.age >= '20' AND ua.age <= '24' AND udt.lookingfor = '1' OR udt.lookingfor = '3';

我想只获得u.gender ='0'用户,但该查询以u.gender ='1'返回给我,那么问题出在哪里?

查询结果:

user_id  gender     age  name         isonline  profile_image_no  
-------  ------  ------  -----------  --------  ------------------
    62       0      29  Wqasaasd            1                   1
    93       1      28  Sadasda             1                   1
    131       0      26  Wedsas              1                   1
    155       0      91  Wwsfwdf             1                   1
    173       0      23  Yirmi30             1                   1
    220       0      20  Fjalekalimi         1                   1
    232       0      20  Seeemani            1                   1
    236       0      21  Mesut               1                   1
    238       0      89  23wdsca             1                   1
    247       0      23  11eqwe              1                   1
    252       0      24  Workhard            1                   1
    253       0      25  Sdsdg               1                   1
    343       1      25  Emily               1                   1

2 个答案:

答案 0 :(得分:2)

OR粘在括号内,然后重试!

SELECT u.user_id,u.gender, ua.age,u.name,u.isonline,dot.profile_image_no FROM user_table AS u
INNER JOIN user_answers_table AS ua ON u.user_id = ua.user_id
INNER JOIN data_one_table AS dot ON dot.user_id = u.user_id
INNER JOIN user_detail_table AS udt ON udt.user_id = u.user_id
WHERE u.isregistrationcomplete='1' 
AND u.isactive='1' 
AND u.gender = '0' 
AND ua.age >= '20' 
AND ua.age <= '24' 
AND (udt.lookingfor = '1' OR udt.lookingfor = '3');

答案 1 :(得分:2)

逻辑语法错误:

AND (udt.lookingfor = '1' OR udt.lookingfor = '3');