[英]Combine 2 rows into one row, Using 2 columns MYSQL
I have a table that looks like this: Table 我有一个看起来像这样的表 :
However, the issue is that instead of saying that Patricia has 13 tickets and 1 movie ticket in the same row, it's separating into different rows. 但是,问题在于,与其说帕特里夏在同一行中有13张票和1张电影票,不如说是分成不同的行。
I think I need to be doing a pivot table, but I'm not sure what exactly I need to be doing. 我想我需要做一个数据透视表,但是我不确定我到底需要做什么。
This is my code so far: 到目前为止,这是我的代码:
select customer.hippcode, customer.LastName, customer.Firstname, customer.Email,
count(ticketdetails.eventtype) as 'Theater Tickets',
0 as 'Movie Tickets'
from customer
inner join ticketdetails on ticketdetails.hippcode = customer.hippcode
where ticketdetails.hippcode is not null
and ticketdetails.eventType ='T'
Group by Customer.hippcode
union
select customer.hippcode, customer.LastName, customer.Firstname, customer.Email,
0, count(ticketdetails.eventtype) as 'Movie Tickets'
from customer
inner join ticketdetails on ticketdetails.hippcode = customer.hippcode
where ticketdetails.hippcode is not null
and ticketdetails.eventType ='M'
Group by Customer.hippcode
order by `theater tickets` + `movie tickets` desc;
Thanks for your help in advance. 感谢您的帮助。
You could try something like this: 您可以尝试这样的事情:
select
customer.hippcode, customer.LastName, customer.Firstname, customer.Email,
count(case when ticketdetails.eventtype = 'T' then 1 else 0 end) as TheaterTickets,
count(case when ticketdetails.eventtype = 'M' then 1 else 0 end) as MovieTickets
from customer
inner join ticketdetails on ticketdetails.hippcode = customer.hippcode
where ticketdetails.hippcode is not null
and ticketdetails.eventType in ('T', 'M')
Group by customer.hippcode, customer.LastName, customer.Firstname, customer.Email
Order by TheaterTickets+MovieTickets desc;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.