1

我有一个联合查询如下:

(SELECT t.id, t.name, c.company AS owner, t.creation_date AS date, t.notes
 FROM 工具 t,客户端 c
 WHERE t.id_customer = '15' AND t.trash_flag = '1')
  联盟
(SELECT f.id, f.name, CONCAT(m.first_name, ' ', m.last_name) AS 所有者, f.date, f.notes
 FROM 文件 f, 元 m
 WHERE ((f.acl = 0) OR (f.acl = 1 AND '1' = TRUE) OR (f.acl = 2 AND f.id = '7')) AND f.id = '15' AND f .trash_flag = '1' AND m.user_id = f.id_user)
 ORDER BY 'name' 'ASC' LIMIT 0,20
一切正常,但我有两个问题:

  1. 如何在给出行号的整个结果集中添加一列
  2. 我可以在不使用 UNION 的情况下执行此操作,例如高级连接吗?

感谢您的时间 MySQL 大师!

4

1 回答 1

4

我现在无法对其进行测试,但根据我的发现,以下可能有效:

参考:行号变量

SQL 语句

SELECT  @rownum := @rownum + 1 rownum
        , t.*
FROM    (
            (SELECT t.id
                    , t.name
                    , c.company AS owner
                    , t.creation_date AS date
                    , t.notes 
            FROM    tool t
                    , client c 
            WHERE   t.id_customer = '15' 
                    AND t.trash_flag = '1' 
            ) UNION (
            SELECT  f.id
                    , f.name
                    , CONCAT(m.first_name, ' ', m.last_name) AS owner
                    , f.date
                    , f.notes 
            FROM    file f
                    , meta m 
            WHERE   ((f.acl = 0) OR (f.acl = 1 AND '1' = TRUE) OR (f.acl = 2 AND f.id = '7')) AND f.id = '15' AND f.trash_flag = '1' AND m.user_id = f.id_user) 
            )
        ) t
        , (SELECT @rownum := 0) r
ORDER BY 
        'name' ASC
LIMIT   0, 20 
于 2011-05-25T09:57:31.643 回答