MYSQL JOIN SELECT 语句 - 省略重复
我想加入以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我理解正确的话,一个简单的 GROUP BY ,仅保留最大订阅类型就可以解决问题。
If I understand you correctly, a simple
GROUP BY
, witholding only the maxium subscription types should do the trick.您可以将查询括起来:
You can bracket your query:
最快的方法是使用临时表。从那里,您可以将第一个查询插入到临时表中,然后仅插入第二个查询中不在临时表中的行,方法是使用临时表的外连接或使用 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.