MySQL 视图不能在 from 中包含子查询,并且不维护顺序。如何根据该查询创建视图?

发布于 2024-11-17 12:12:26 字数 416 浏览 1 评论 0原文

我本质上是想获得每个员工当前头衔的结果集。我想从中创建一个视图以供以后使用,但我发现我被难住了,并且可能缺少一个简单的解决方案。这是有问题的查询,提前致谢!

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泛滥成性 2024-11-24 12:12:26

更新:

SELECT
    appointment.employee_id ,
    ( SELECT title.`name`
      FROM appointment_title
        INNER JOIN title
          ON appointment_title.title_id = title.id
      WHERE appointment.id = appointment_title.appointment_id
      ORDER BY appointment_title.effective_date DESC
      LIMIT 1
    ) AS title_name
FROM appointment
GROUP BY appointment.employee_id

Updated:

SELECT
    appointment.employee_id ,
    ( SELECT title.`name`
      FROM appointment_title
        INNER JOIN title
          ON appointment_title.title_id = title.id
      WHERE appointment.id = appointment_title.appointment_id
      ORDER BY appointment_title.effective_date DESC
      LIMIT 1
    ) AS title_name
FROM appointment
GROUP BY appointment.employee_id
时光匆匆的小流年 2024-11-24 12:12:26

另一种选择是将查询分解为两个视图。第一个视图将包含派生表子查询,第二个视图将简单地从该子查询中进行选择:

CREATE VIEW vwEmployee_Inner AS
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

然后您的原始视图将变为:

CREATE VIEW vwEmployee AS
SELECT * FROM vwEmployee_Inner GROUP BY employee_id

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:

CREATE VIEW vwEmployee_Inner AS
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

And then your original view becomes:

CREATE VIEW vwEmployee AS
SELECT * FROM vwEmployee_Inner GROUP BY employee_id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文