带别名问题的MYSql

时间:2012-03-21 20:40:16

标签: mysql alias

指的是:http://pastebin.com/raHbAvRN

我的桌子:

Users:(id, lat, lon, ...)
Favorites:(id, userId, prefId)

“我的”别名有效,但“赞”不是别名,你能帮我理解为什么吗? 我需要使用true / false填充输出中的“favorite”列,具体取决于当前“users.id”是否在pref ID列表中。

谢谢

1 个答案:

答案 0 :(得分:0)

试试这样:

select count(*),
    users.id,
    ...distance(my.lat, my.lon, users.lat, users.lon) as distance,
    case when users.id in (
        select favorites.prefId
       from favorites
       where favorites.userId = ?) then true else false end as favorite
from users,
    (
        select users.lat,
            users.lon
        from users
        where users.id = ?
        ) as my
where...