0

我正在尝试获取事件的城市名称和用户居住地的城市名称。它们都有一个邮政编码,对应于表中的 city_name cities

到目前为止我的查询:

SELECT
    meetups.meetup_name,
    meetups.meetup_text,
    users.username,
    cities.city_name
FROM
    meetups, users, cities
WHERE
    meetups.url_meetup = ? and users.id = meetups.author_id and cities.postalcode = meetups.postalcode

我仍然需要得到用户的城市名称。

我的表格如下所示:

meetups =>
    id
    meetup_url
    meetup_name
    meetup_text
    author_id
    postalcode

users =>
    id
    username
    postalcode

cities =>
    postalcode
    city_name
4

2 回答 2

1

试试这个:

SELECT
meetups.meetup_name,
meetups.meetup_text,
users.username,
meetups.postalcode as mPC,
(select cities.city_name from cities where cities.postalcode = mPC) as mCity,
users.postalcode as uPC,
(select cities.city_name from cities where cities.postalcode = uPC) as uCity,
FROM
meetups, users, cities
WHERE
meetups.url_meetup = ?
AND users.id = meetups.author_id;
于 2013-04-17T23:44:54.650 回答
1
SELECT
    meetups.meetup_name,
    meetups.url_meetup,
    meetups.meetup_text,
    users.username,
    c1.city_name as meetup_city,
    c2.city_name as user_city   
FROM
meetups, users, cities c1, cities c2   
WHERE
url_meetup = ? 
and users.id = meetups.author_id 
and c1.postalcode = meetups.postalcode
and c2.postalcode = users.postalcode

这也应该让你得到用户城市的名称。

于 2013-04-17T23:54:59.270 回答