1

可能重复:
每个派生表都必须有自己的别名

晕,我正在尝试获取一个表记录,它可以通过组合一个表的两个结果来显示结果。在我的表“updatez”中,我正在存储来自用户的所有更新(朋友表存储用户对朋友的可访问性)和更换器(更换器表存储更换器上的用户可访问性)到此表中......在这里我想通过对创建的时间和订单0,5进行排序来显示所有朋友和更换器更新(如果他们被批准)......

有没有其他更简单的解决方案来做到这一点我正在尝试的代码

SELECT *
FROM (
        SELECT M.updt_id AS updt_id,
                M.user_id_fk AS user_id_fk,
                M.updates AS updates,
                M.created AS created,
                M.uploads AS uploads
        FROM updatez M
        WHERE updatez.user_id_fk = friends.friendA
                AND friends.friendB = $USER

        UNION

        SELECT A.updt_id AS updt_id,
                changer_id_fk AS changer_id_fk,
                A.updates AS updates,
                A.created AS created,
                A.uploads AS uploads
        FROM updatez A
        WHERE updatez.changer_id_fk = changer.changer_id
                AND changer.user_id = $USER
        ) 
ORDER BY created DESC 
LIMIT 0, 5

任何形式的帮助将不胜感激.. 提前谢谢蒂姆

4

1 回答 1

5

你只需要添加ALIAS子查询

SELECT *
FROM (
        SELECT M.updt_id AS updt_id,
                M.user_id_fk AS user_id_fk,
                M.updates AS updates,
                M.created AS created,
                M.uploads AS uploads
        FROM updatez M
        WHERE updatez.user_id_fk = friends.friendA
                AND friends.friendB = $USER

        UNION

        SELECT A.updt_id AS updt_id,
                changer_id_fk AS changer_id_fk,
                A.updates AS updates,
                A.created AS created,
                A.uploads AS uploads
        FROM updatez A
        WHERE updatez.changer_id_fk = changer.changer_id
                AND changer.user_id = $USER
        ) derivedTable                            // -- <<< HERE
ORDER BY created DESC 
LIMIT 0, 5
于 2012-11-27T16:51:42.397 回答