0

我试图做一个看起来像这样的查询:

SELECT `online`.user_id, users.profile_image_url, profile_info.profile_text, users.kind_of_relationship, users.gender_search, users.country
FROM `online`, users, profile_info 
WHERE (`online`.user_id = users.id)
AND (`online`.user_id = profile_info.profile_user_id)
AND (users.user_age >= '25' AND users.user_age <= '38')
AND (users.kind_of_relationship = 'Friendship')
AND (users.gender = 'female')
AND users.country IN (SELECT searching_countries.country FROM searching_countries, `online` WHERE `online`.user_id = searching_countries.user_id);

它失败了。

但是,如果我像这样运行此查询

SELECT `online`.user_id, users.profile_image_url, profile_info.profile_text, users.kind_of_relationship, users.gender_search, users.country
FROM `online`, users, profile_info 
WHERE (`online`.user_id = users.id)
AND (`online`.user_id = profile_info.profile_user_id)
AND (users.user_age >= '25' AND users.user_age <= '38')
AND (users.kind_of_relationship = 'Friendship')
AND (users.gender = 'female')
AND users.country IN ('Sweden', 'Austria');

然后查询工作正常。

另外,如果我只运行查询:

SELECT searching_countries.country FROM searching_countries, `online` WHERE `online`.user_id = searching_countries.user_id 

我得到结果瑞典,奥地利..

任何想法为什么这个IN(SELECT search_countries.country FROM search_countries, onlineWHERE online.user_id = search_countries.user_id) 不想工作?

先感谢您!

4

3 回答 3

0

我认为这成功了,这就是我现在的解决方案。

对不起,如果有任何误解,但我仍然想分享我是如何通过做一个 in 来解决我的问题的。事情有时不需要那么复杂:)

SELECT `online`.user_id, users.user_name, users.profile_image_url, profile_info.profile_text, users.kind_of_relationship, users.gender_search, users.country
FROM `online`, users, profile_info  
WHERE (`online`.user_id = users.id)
AND (`online`.user_id = profile_info.profile_user_id)
AND (users.user_age >= '25' AND users.user_age <= '38')
AND (users.kind_of_relationship = 'Friendship')
AND (users.gender = 'female')
AND users.country IN (SELECT country FROM searching_countries WHERE user_id ='55');
于 2012-10-10T12:50:20.190 回答
0
UNION SELECT `online`.user_id, users.profile_image_url, profile_info.profile_text, users.kind_of_relationship, users.gender_search, users.country FROM `online`, users, profile_info  WHERE (`online`.user_id = users.id) AND (`online`.user_id = profile_info.profile_user_id) AND (users.user_age >= '25' AND users.user_age <= '38') AND (users.kind_of_relationship = 'Friendship') AND (users.gender = 'female') AND users.country IN ('Sweden', 'Austria');
于 2012-10-10T09:17:49.417 回答
0

ALIAS在子查询中为online表添加一个。

SELECT ...
FROM...
WHERE....
...AND (users.gender = 'female')
AND users.country IN
(
SELECT searching_countries.country 
FROM searching_countries, `online` x
WHERE x.user_id = searching_countries.user_id
)
于 2012-10-10T09:18:10.117 回答