我在完成 MySQL 查询时遇到了一些麻烦。想象一下,我将表格票、发票行和用户结合起来。每张票都有一个或多个发票行,每个用户可以拥有一张或多张票。
我正在尝试创建一个可用于创建姓名徽章的查询。对于这个徽章,我需要知道该人注册的姓名和物品。
我创建了以下 MySQL 查询
SELECT u.firstName, u.lastName, il.invID, il.name,
CASE
WHEN il.name = 'Conference Dinner'
THEN 'Diner'
END AS 'conference_dinner',
CASE
WHEN il.name = 'Regular Conference Fee' THEN 'Conference'
WHEN il.name = 'Conference Fee for Phd. Students' THEN 'Conference'
END AS 'conference',
CASE
WHEN il.name = 'Pre-Conference Fee' THEN 'Pre-Conference'
END AS 'pre_conference',
FROM invoice_line il, events_tickets et, users u
WHERE il.invID = et.invID
AND et.userID = u.ID
查询几乎没问题,但是它为具有三个发票行(= 三个项目)的用户生成了表中的三行,我希望这三行合并为一行。用户 ID 上的 Group By 将产生一行,但比案例创建的列不会合并到一行中,只显示其中一个。
我可能使用了错误的方法来做到这一点。但它应该产生一个像这样的表:
firstName | lastName | conference_diner | conference | pre_conference |
----------------------------------------------------------------------
John | Doe | Diner | Conference | |
Jane | Norman | | Conference | Pre-Conference |
Martijn | Thomas | | Conference | |
Pete | Johns | | | Pre-Conference |
目前这导致:
firstName | lastName | conference_diner | conference | pre_conference |
----------------------------------------------------------------------
John | Doe | Diner | | |
John | Doe | | Conference | |
Jane | Norman | | Conference | |
Jane | Norman | | | Pre-Conference |
Martijn | Thomas | | Conference | |
Pete | Johns | | | Pre-Conference |
提前致谢