我有以下查询返回2个元组
SELECT bar_id, bar_name, town_name, bar_telephone, subscription_type_id, pic_type
FROM towns, subscriptiontype, regions, bar
LEFT JOIN barpictures bp ON bar.bar_id = bp.bar_id_fk
WHERE town_id = town_id_fk
AND bar.test_field = 0
AND subscription_type_id = subscription_type_id_fk
AND region_id = region_id_fk
AND (type like 'logo%' OR type IS NULL)
元组之间的主要区别在于,其中一个具有'type'= logo而另一个元组具有'type'= logo_large。我需要它而不是有两个元组,我需要我有2个类型属性,一个拿着“logo”,另一个拿着“logo_large”
例如
bar_id, bar_name, town_name, bar_telephone, subscription_type_id, pic_type1, pic_type2
这可能吗?
答案 0 :(得分:1)
是的,有可能:
SELECT ..., subscription_type_id,
MAX(IF(type='logo',type,NULL)) as pic_type1,
MAX(IF(type='logo_large',type,NULL)) as pic_type2
...
GROUP BY bar_id;
我们的想法是使用聚合函数来合并行,并仅选择与您的条件匹配的值(对于非匹配行,聚合函数应返回null)