gpt4 book ai didi

MYSQL JOIN SELECT 语句 - 省略重复项

转载 作者:行者123 更新时间:2023-11-29 05:45:23 25 4
gpt4 key购买 nike

我想加入以下 2 个查询,但我重复了....可以从这里删除重复的查询:

(
SELECT bar_id, bar_name, town_name, bar_telephone,
(subscription_type_id *2) AS subscription_type_id
FROM bar, sportactivitybar, towns, subscriptiontype
WHERE sport_activity_id_fk =14
AND bar_id = bar_id_fk
AND town_id = town_id_fk
AND subscription_type_id = subscription_type_id_fk
)
UNION
(
SELECT bar_id, bar_name, town_name, bar_telephone,
subscription_type_id
FROM bar, towns, subscriptiontype
WHERE town_id = town_id_fk
AND subscription_type_id = subscription_type_id_fk
)
ORDER BY subscription_type_id DESC , RAND( )

请注意,我需要忽略那些具有较低 subscription_type_id 的重复项

最佳答案

如果我没理解错的话,一个简单的 GROUP BY,只保留 maxium 订阅类型应该可以解决问题。

SELECT  dupAlias.bar_id
, dupAlias.bar_name
, dupAlias.town_name
, dupAlias.bar_telephone
, MAX(dupAlias.subscription_type_id) AS subscription_type_id
FROM (
SELECT bar_id
, bar_name
, town_name
, bar_telephone
, (subscription_type_id *2) AS subscription_type_id
FROM bar
, sportactivitybar
, towns
, subscriptiontype
WHERE sport_activity_id_fk =14
AND bar_id = bar_id_fk
AND town_id = town_id_fk
AND subscription_type_id = subscription_type_id_fk
UNION
SELECT bar_id
, bar_name
, town_name
, bar_telephone
, subscription_type_id
FROM bar
, towns
, subscriptiontype
WHERE town_id = town_id_fk
AND subscription_type_id = subscription_type_id_fk
) dupAlias
GROUP BY
dupAlias.bar_id, dupAlias.bar_name, dupAlias.town_name, dupAlias.bar_telephone
ORDER BY
dupAlias.subscription_type_id DESC , RAND( )

关于MYSQL JOIN SELECT 语句 - 省略重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2789398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com