我有下面的桌子:
服务
╔════╦═════════════╗
║ ID ║ DESCRIPTION ║
╠════╬═════════════╣
║ 1 ║ A ║
║ 2 ║ B ║
║ 3 ║ C ║
╚════╩═════════════╝
工作人员
╔════╦══════╗
║ ID ║ NAME ║
╠════╬══════╣
║ 1 ║ ABC ║
║ 2 ║ DEF ║
║ 3 ║ GHI ║
╚════╩══════╝
员工服务
╔══════════╦═════════════╦═════╗
║ STAFF_ID ║ SERVICES_ID ║ QTY ║
╠══════════╬═════════════╬═════╣
║ 1 ║ 1 ║ 2 ║
║ 1 ║ 3 ║ 3 ║
║ 2 ║ 3 ║ 1 ║
║ 3 ║ 2 ║ 4 ║
╚══════════╩═════════════╩═════╝
如何查询所有这些表以获得如下结果?
结果:
╔══════╦═════════════╦════════╗
║ NAME ║ DESCRIPTION ║ QTY ║
╠══════╬═════════════╬════════╣
║ ABC ║ A ║ 2 ║
║ ABC ║ B ║ (null) ║
║ ABC ║ C ║ 3 ║
║ DEF ║ A ║ (null) ║
║ DEF ║ B ║ (null) ║
║ DEF ║ C ║ 1 ║
║ GHI ║ A ║ (null) ║
║ GHI ║ B ║ 4 ║
║ GHI ║ C ║ (null) ║
╚══════╩═════════════╩════════╝
谢谢你的帮助
最佳答案
SELECT a.Name, b.Description, c.Qty
FROM Staff a
CROSS JOIN Services b
LEFT JOIN StaffServices c
ON a.ID = c.Staff_ID AND
b.ID = c.Services_ID
ORDER BY a.Name, b.Description
SQLFiddle Demo
关于mysql - 在MySQL上联接多个表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14982231/