Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Tambahkan kolom baru ke hasil dan gabungkan tabel

Saya pikir Anda perlu union all :

select id, campaign, event_type, 'delivered' as sub_event_type, date_delivered
from logistics
where event_type = 'stored' and date_delivered is not null
union all
select id, campaign, event_type, 'recorded' as sub_event_type, date_recorded
from logistics
where event_type = 'stored' and date_recorded is not null
union all
select id, campaign, event_type, 'completed' as sub_event_type, date_completed
from logistics
where event_type = 'stored' and date_completed is not null
union all
select id, campaign, event_type, null as sub_event_type, date_offered
from logistics
where event_type = 'offered' 
union all
select id, campaign, event_type, null as sub_event_type, date_ordered
from logistics
where event_type = 'ordered' ;



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. sql bergabung sebagai diagram venn

  2. subsistem windows instal server mysql

  3. Bagaimana menghubungkan ke MySQL yang berjalan di Docker dari mesin host

  4. Buat kelas C# berdasarkan tabel MySQL

  5. Mengapa MySQL mengizinkan grup berdasarkan kueri TANPA fungsi agregat?