简体   繁体   English

MySQL SELECT语句问题

[英]MySQL SELECT Statment issue

I have the following query which returns 2 tuples 我有以下查询返回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) 

The main difference between the tuples is that one has 'type' = logo and the other tuple has 'type' = logo_large. 元组之间的主要区别是,一个元组具有“ type” =徽标,而另一个元组具有“ type” = logo_large。 I need that instead of having two tuples, I need that I have 2 type attributes, one holding the "logo" and the other the "logo_large" 我需要而不是拥有两个元组,而是需要拥有2个类型属性,一个属性包含“徽标”,另一个属性包含“ logo_large”

eg 例如

bar_id, bar_name, town_name, bar_telephone, subscription_type_id, pic_type1, pic_type2

is this possible? 这可能吗?

Yes, it is possible: 对的,这是可能的:

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;

The idea is to use aggregating functions to merge the rows and select only the value that matches your condition (for non-matching rows the aggregating function should return null) 这个想法是使用聚合函数合并行并仅选择与您的条件匹配的值(对于不匹配的行,聚合函数应返回null)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM