2011-06-28 4 views
0

私は基本的に各従業員の現在のタイトルで結果セットを取得しようとしています。私は後で使用するためにこれからビューを作成したいと思いますが、私は困惑していて、簡単な解決策がない可能性が高いと感じています。問題のクエリはありますが、事前に感謝しています!更新MySQLビューはfromのサブクエリを持つことはできません。また、orderは維持しません。このクエリからビューを作成するにはどうすればよいですか?

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 

答えて

0

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 
0

別のオプションは二つのビューにクエリを分割することです。最初のビューは、派生テーブルのサブクエリが含まれます、第二は、単にそのいずれかから選択します:

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