MySQL 视图不能在 from 中包含子查询,并且不维护顺序。如何根据该查询创建视图?
我本质上是想获得每个员工当前头衔的结果集。我想从中创建一个视图以供以后使用,但我发现我被难住了,并且可能缺少一个简单的解决方案。这是有问题的查询,提前致谢!
select * from
(SELECT
appointment.employee_id,
title.`name` as title_name
FROM
appointment
INNER JOIN appointment_title ON appointment.id = appointment_title.appointment_id
INNER JOIN title ON appointment_title.title_id = title.id
order by appointment_title.effective_date DESC) tmp group by employee_id
I'm essentially trying to obtain a resultset with each employee's current title. I'd like to create a view from this for later use, but I find I'm being stumped, and likely missing a simple solution. Here's the query in question, and thanks in advance!
select * from
(SELECT
appointment.employee_id,
title.`name` as title_name
FROM
appointment
INNER JOIN appointment_title ON appointment.id = appointment_title.appointment_id
INNER JOIN title ON appointment_title.title_id = title.id
order by appointment_title.effective_date DESC) tmp group by employee_id
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新:
Updated:
另一种选择是将查询分解为两个视图。第一个视图将包含派生表子查询,第二个视图将简单地从该子查询中进行选择:
然后您的原始视图将变为:
Another option is to break up the query into two views. The first view will contain the derived table subquery, and the second will simply select from that one:
And then your original view becomes: