MYSQL JOIN SELECT 语句 - 省略重复

发布于 2024-08-31 21:16:08 字数 682 浏览 2 评论 0原文

我想加入以下 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 的重复项

I am tying to join the following 2 queries but I am having duplicated .... it is possible to remove duplacted fro this:

(
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( )

Please note that I need to omit those duplicates that will have a lower subscription_type_id

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

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

发布评论

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

评论(3

尤怨 2024-09-07 21:16:08

如果我理解正确的话,一个简单的 GROUP BY ,仅保留最大订阅类型就可以解决问题。

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( )

If I understand you correctly, a simple GROUP BY, witholding only the maxium subscription types should do the trick.

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( )
梦行七里 2024-09-07 21:16:08

您可以将查询括起来:

SELECT bar_id, bar_name, town_name, bar_telephone, 
    max(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
    )
) x
GROUP BY bar_id, bar_name, town_name, bar_telephone
ORDER BY subscription_type_id DESC , RAND( )

You can bracket your query:

SELECT bar_id, bar_name, town_name, bar_telephone, 
    max(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
    )
) x
GROUP BY bar_id, bar_name, town_name, bar_telephone
ORDER BY subscription_type_id DESC , RAND( )
天荒地未老 2024-09-07 21:16:08

最快的方法是使用临时表。从那里,您可以将第一个查询插入到临时表中,然后仅插入第二个查询中不在临时表中的行,方法是使用临时表的外连接或使用 not在声明中。或者,您可以插入所有第二个查询,并在临时表中的选择中使用 group by 子句。

The fastest way would be to use a temp table. And them from there you could do insert the first query into the temp table and then only insert those rows you want from the second query that aren't in the temp table either by using an outer join to the temp table or by using a not in statement. Or you could insert all of the second query and just use a group by clause in your select from the temp table.

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