2014-01-29 83 views
0

我有一個小問題與MySQL。查詢是如下:MySQL多重內部加入Where子句

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 

回答

3

把你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'); 
+0

感謝這個查詢解決我的問題 – Mesuti

2

壞邏輯句法:

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