将 2 行合并为一行,使用 2 列 MYSQL

Combine 2 rows into one row, Using 2 columns MYSQL

我有一个 table 看起来像这样: Table

但是,问题是帕特里夏没有说同一行有 13 张票和 1 张电影票,而是分成不同的行。

我想我需要做一个关键点 table,但我不确定我到底需要做什么。

到目前为止,这是我的代码:

    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;

提前感谢您的帮助。

您可以尝试这样的操作:

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;