如何在 MySQL 中连接整个结果集?
我正在尝试以下查询:
SELECT A,B,C FROM table WHERE field LIKE 'query%'
UNION
SELECT A,B,C FROM table WHERE field LIKE '%query'
UNION
SELECT A,B,C FROM table WHERE field LIKE '%query%'
GROUP BY B ORDER BY B ASC LIMIT 5
这是三个查询粘在一起,有点像。 但是,返回的结果集在查询 #1 的结果之前反映了查询 #3 的结果,这是不希望的。
有什么方法可以优先考虑这些,以便结果全部针对查询#1,然后全部针对查询#2,最后全部针对查询#3? 我还不想在 PHP 中执行此操作(更不用说必须控制第一个查询中显示的结果不显示在第二个查询中,依此类推)。
I'm trying out the following query:
SELECT A,B,C FROM table WHERE field LIKE 'query%'
UNION
SELECT A,B,C FROM table WHERE field LIKE '%query'
UNION
SELECT A,B,C FROM table WHERE field LIKE '%query%'
GROUP BY B ORDER BY B ASC LIMIT 5
That's three queries stuck together, kinda sorta. However, the result set that comes back reflects results from query #3 before the results from query #1 which is undesired.
Is there any way to prioritize these so that results come as all for query #1, then all for query #2 then all for query #3? I don't want to do this in PHP just yet (not to mention having to control for results that showed up in the first query not to show in the second and so forth).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
选择不同的 a,b,c FROM (
SELECT A,B,C,1 as o FROM 表 WHERE 字段 LIKE 'query%'
联盟
SELECT A,B,C,2 as o FROM 表 WHERE 字段 LIKE '%query'
联盟
SELECT A,B,C,3 as o FROM 表 WHERE 字段 LIKE '%query%'
)
ORDER BY o ASC LIMIT 5
将是我的做法。 我不知道这是如何扩展的。
我不明白它
是否仅适用于联合中的最后一个 SELECT?
mysql 是否真的允许您按列进行分组,但仍然不能对其他列进行聚合?
编辑:啊啊啊。 我发现mysql确实是这么做的。 它是 DISTINCT(b) 之类的特殊版本。 我不想尝试成为该领域的专家:)
SELECT distinct a,b,c FROM (
SELECT A,B,C,1 as o FROM table WHERE field LIKE 'query%'
UNION
SELECT A,B,C,2 as o FROM table WHERE field LIKE '%query'
UNION
SELECT A,B,C,3 as o FROM table WHERE field LIKE '%query%'
)
ORDER BY o ASC LIMIT 5
Would be my way of doing it. I dont know how that scales.
I don't understand the
Does it apply only to the last SELECT in the union?
Does mysql actually allow you to group by a column and still not do aggregates on the other columns?
EDIT: aaahh. I see that mysql actually does. Its a special version of DISTINCT(b) or something. I wouldnt want to try to be an expert on that area :)
如果没有一种有意义的排序方式可以对它们进行排序,则不要将结果合并在一起 - 只需返回 3 个单独的记录集,并在数据层中相应地处理它们。
If there isn't a sort that makes sense to order them you desire, don't union the results together - just return 3 separate recordsets, and deal with them accordingly in your data tier.
我最终(查看所有建议)找到了这个解决方案,它是我需要的和时间之间的一个妥协。
它总共提供了 5 个结果,从第四“列”排序并给出了我需要的内容; 一个自然结果集(它来自 AJAX),紧随其后的是一个通配符结果集。
:)
/mp
I eventually (looking at all suggestions) came to this solution, its a bit of a compromise between what I need and time.
it delivers 5 results total, sorts from the fourth "column" and gives me what I need; a natural result set (its coming over AJAX), and a wildcard result set following right after.
:)
/mp
UNION 有两种变体。
在大多数情况下,您真正想说的是 UNION ALL,因为它不会在集合之间进行重复消除(想想
SELECT DISTINCT
),这可能会节省大量执行时间。其他人建议使用多个结果集,这是一个可行的解决方案,但我会警告在时间敏感的应用程序或通过 WAN 连接的应用程序中不要这样做,因为这样做可能会导致服务器和客户端之间的线路上的往返次数显着增加。
There are two varients of UNION.
In most cases what you really want to say is UNION ALL as it does not do duplicate elimination (Think
SELECT DISTINCT
) between sets which can result in quite a bit of savings in terms of execution time.Others have suggested multiple result sets which is a workable solution however I would caution against this in time sensitive applications or applications connected over WANs as doing so can result in significantly more round trips on the wire between server and client.
也许您应该尝试包含第四列,说明它来自的表,然后按它进行排序和分组:
Maybe you should try including a fourth column, stating the table it came from, and then order and group by it:
添加一个包含硬编码值的附加列,您将使用它对整体结果集进行排序,如下所示:
Add an additional column with hard-coded values that you will use to sort the overall resultset, like so:
你能把它作为子选择吗,比如
Can you do it as a subselect, something like
我不明白为什么需要联合从单个表中获取数据
I don't understand why the need of union for taking the data from single table