MySQL SELECT 语句问题

发布于 2024-08-31 17:04:52 字数 653 浏览 4 评论 0原文

我有以下查询,它返回 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。我需要的不是两个元组,而是两个类型属性,一个包含“logo”,另一个包含“logo_large”,

例如

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

这可能吗?

I have the following query which returns 2 tuples

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. 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"

eg

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

is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栀梦 2024-09-07 17:04:52

是的,这是可能的:

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)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文