[英]MySQL query rows to columns
使用條件聚合:
select
p.id,
max(case when cf.title = 'Rental (FSG)' then cfv.value end) rental_fsg,
max(case when cf.title = 'Model Year' then cfv.value end) model_year,
max(case when cf.title = 'Product' then cfv.value end) product,
max(case when cf.title = 'Partner' then cfv.value end) partner,
max(case when cf.title = 'SAP delivery / invoice number' then cfv.value end) sap_delivery
from custom_fields cf
join custom_field_values cfv on cfv.custom_field_id = cf.id
join projects p on p.id = cfv.related_to_id
where p.deleted = 0
group by p.id
請注意,我修改了您的查詢以使用表別名( cf
、 cfv
、 p
):它們使查詢更易於編寫和閱讀。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.