MySQL 查询:使用 UNION 并获取行号作为 SELECT 的一部分
我有一个联合查询如下:
(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) ORDER BY 'name' 'ASC' LIMIT 0,20Everything works fine but I have two questions:
- 如何向整个结果集中添加一个给出行号的列
- 我可以在不使用 UNION(例如高级联接)的情况下执行此操作吗?
感谢 MySQL 专家们的宝贵时间!
I have a union query as follows:
(SELECT t.id, t.name, c.company AS owner, t.creation_date AS date, t.notesEverything works fine but I have two questions:
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)
ORDER BY 'name' 'ASC' LIMIT 0,20
- How do I add a column to the entire result set that gives the row number
- Could I do this without using UNION e.g. an advanced join?
Thanks for your time MySQL gurus!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我现在无法测试它,但从我发现的情况来看,以下可能有效:
参考:行号变量
SQL语句
I can't test it righ now but from what I found, following might work:
Reference: Row Number Variable
SQL Statement