好的,我可能在搜索中错过了这个,但我不确定我在寻找什么。我有几个表,我需要从中生成邮件标签。主表有姓名。然后第二个表有多行数据,如phon-123415324,地址1- 1 james st等...还有一个绑定数据的表。这是一个示例结果和查询。
SELECT
users.name,
users.surname,
details.`field`,
details.value
FROM
users
Inner Join details ON details.user_id = users.id
Inner Join bookings ON bookings.guest_id = users.id
WHERE
bookings.sub_event_id = '78'
NAME, SURNAME, field, value
David Oden title Mr
David Oden sex Male
David Oden mobile 0534600594
David Oden company Fterns Group
David Oden position
David Oden address_1 Cnr wtrewr Rd & wert St
David Oden address_2
David Oden suburb wertt Mile wertw
David Oden state MAS
David Oden postcode 14113
David Oden country USA
我需要什么我将所有数据导出到excel用于邮寄标签。对不起,如果这真的很蠢。我花了大约4个小时研究没有运气。
我需要将类型作为列标题,将值作为数据。
Shoud read
Title | Name | Surname | Mobile | Company | Address etc.....
MR | David | Oden | 0534600594 | Fterns Group | etc....
任何帮助将不胜感激/
答案 0 :(得分:1)
这被称为PIVOT
函数,但遗憾的是MySQL没有PIVOT
,所以你必须使用聚合函数和CASE
语句来复制它。如果您知道需要在查询中包含的值,那么您可以对它们进行硬编码,类似于以下内容:
select
min(case when d.field = 'title' then d.value end) as Title,
u.name,
u.surname,
min(case when d.field = 'sex' then d.value end) as Sex,
min(case when d.field = 'mobile' then d.value end) as Mobile,
min(case when d.field = 'company' then d.value end) as Company,
min(case when d.field = 'position' then d.value end) as Position,
min(case when d.field = 'address_1' then d.value end) as Address_1,
min(case when d.field = 'address_2' then d.value end) as Address_2,
min(case when d.field = 'suburb' then d.value end) as suburb,
min(case when d.field = 'state' then d.value end) as State,
min(case when d.field = 'postcode' then d.value end) as Postcode,
min(case when d.field = 'country' then d.value end) as country
from users u
left join details d
on u.id = d.id
left join bookings b
on u.id = b.guest_id
where b.sub_event_id = 78
group by u.name, u.surname
但是如果您的列数未知或者值会发生变化,那么您将需要动态执行此操作,然后您应该阅读以下有关预准备语句的文章:
Dynamic pivot tables (transform rows to columns)
您的代码如下所示:
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'min(case when d.field = ''',
d.field,
''' then d.value end) AS ',
d.field
)
) INTO @sql
from users u
left join details d
on u.id = d.d_id;
SET @sql = CONCAT('SELECT u.name,
u.surname, ', @sql, '
from users u
left join details d
on u.id = d.d_id
left join bookings b
on u.id = b.guest_id
where b.sub_event_id = 78
group by u.name, u.surname');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;