0

我正在制作一个网站,用户可以在其中添加他们访问过的地方。

有4张主桌

users (user_id,name )
places (place_id,type_id,place_name, city_id)
user_place (user_id,place_id, date )
city(city_id,city_name)

现在我需要查询所有类型为 2 的地方,(where type=2)其中给定用户参与(user_id=43 lets say)的地方名称、城市名称和所有其他用户的数量也参与同一个地方......昨天带着一本关于 mysql 的书,我遇到了这样的事情

    SELECT *
    FROM `user_place` , `places`,(SELECT count(user_id) 
 from user_places
 WHERE user_place.place_id = places.place_id) as count
    WHERE user_place.place_id = places.place_id
    AND user_place.user_id =53

但它给出的错误:'where子句'中的未知列'places.place_id'并且仍然不知道我怎样才能巧妙地将地名和城市名称附加到结果中......如果可以请帮助......

4

2 回答 2

1

你可以看看这样的东西

SELECT  u.user_id,
        u.NAME,
        p.place_id,
        p.place_name,
        c.city_id,
        c.city_name,
        placeCount.Cnt
FROM    places p INNER JOIN
        user_place up ON p.place_id = up.place_id INNER JOIN
        users u ON up.user_id = u.user_id INNER JOIN
        city c ON p.city_id = c.city_id LEFT JOIN
        (
            SELECT  place_id,
                    COUNT(DISTINCT user_id) Cnt
            FROM    user_place
            WHERE   user_id <> 43
            GROUP BY place_id
        ) placeCount ON p.place_id = placeCount.place_id
WHERE   p.type_id = 2
AND     up.user_id = 43
于 2009-12-28T12:09:27.077 回答
0
SELECT p.place_id, p.place_name, c.city, placeCount.Cnt
FROM places p
INNER JOIN user_place up ON p.place_id = up.place_id
INNER JOIN fa_user u ON up.user_id = u.id
INNER JOIN city c ON p.city_id = c.id
LEFT JOIN (

SELECT place_id, COUNT( DISTINCT user_id ) Cnt
FROM user_place
WHERE user_id <>53
GROUP BY place_id
)placeCount ON p.place_id = placeCount.place_id
WHERE up.user_id =53
LIMIT 0 , 30
于 2009-12-28T20:36:44.337 回答