Mysql
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> Mysql

Thêm cột mới vào bảng kết quả và kết hợp

Tôi nghĩ bạn cần 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. Chuẩn bị câu lệnh MySQL INSERT / UPDATE với các giá trị DEFAULT

  2. script để chuyển đổi tệp sql mysql dump thành định dạng có thể được nhập vào sqlite3 db

  3. Làm thế nào để tăng trong SQL bằng cách sử dụng một trình kích hoạt?

  4. Hiểu việc sử dụng bộ nhớ của MySQL dẫn đến kết quả trong PHP (PDO)

  5. MySQL:Chọn thực thi truy vấn và thời gian tìm nạp kết quả tăng lên theo số lượng kết nối