mysql多关系?

ijxebb2r  于 2021-06-23  发布在  Mysql
关注(0)|答案(3)|浏览(350)

我有一个表employee(employee\u id,employee\u name)、tour(tour\u id,tour\u name)、employee\u tour(tour\u id,employee\u id)。
我怎样才能让所有的员工加入所有的旅游?

n3schb8v

n3schb8v1#

试试看。。

select employee_name from
(SELECT    employee_id, COUNT(*) AS total FROM employee_tour 
        GROUP BY  employee_id) as tb 
inner join employee on tb.id_employee = employee.id_employee
where tb.total = (select count (*) from tour)
hi3rlvi2

hi3rlvi22#

我将使用两个联接—一个用于统计所有旅行的聚合查询,另一个用于统计员工参加的旅行次数的聚合查询,然后比较它们:

SELECT     e.*
FROM       employee e
JOIN       (SELECT    employee_id, COUNT(*) AS employee_tours
            FROM      employee_tour
            GROUP BY  employee_id) et ON e.employee_id = et.employee_id
CROSS JOIN (SELECT COUNT(*) AS all_tours 
            FROM tour) t
WHERE      employee_tours = all_tours
pgvzfuti

pgvzfuti3#

可以使用join语句

SELECT e.*, t.*
FROM Employee e
INNER JOIN employee_tour et ON et.employee_id = e.employee_id
INNER JOIN tour t ON t.tour_id = et.tour_id

请参阅加入

相关问题