如何在 MySQL 中连接整个结果集?

发布于 2024-07-04 01:55:50 字数 424 浏览 6 评论 0原文

我正在尝试以下查询:

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 技术交流群。

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

发布评论

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

评论(8

梦屿孤独相伴 2024-07-11 01:55:50

选择不同的 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

将是我的做法。 我不知道这是如何扩展的。

我不明白它

GROUP BY B ORDER BY B 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

GROUP BY B ORDER BY B ASC LIMIT 5

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

过期以后 2024-07-11 01:55:50

如果没有一种有意义的排序方式可以对它们进行排序,则不要将结果合并在一起 - 只需返回 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.

油焖大侠 2024-07-11 01:55:50

我最终(查看所有建议)找到了这个解决方案,它是我需要的和时间之间的一个妥协。

SELECT * FROM 
  (SELECT A, B, C, "1" FROM table WHERE B LIKE 'query%' LIMIT 3
   UNION
   SELECT A, B, C, "2" FROM table WHERE B LIKE '%query%' LIMIT 5)
AS RS
GROUP BY B
ORDER BY 1 DESC

它总共提供了 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.

SELECT * FROM 
  (SELECT A, B, C, "1" FROM table WHERE B LIKE 'query%' LIMIT 3
   UNION
   SELECT A, B, C, "2" FROM table WHERE B LIKE '%query%' LIMIT 5)
AS RS
GROUP BY B
ORDER BY 1 DESC

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

很快妥协 2024-07-11 01:55:50

UNION 有两种变体。

'UNION' and 'UNION ALL'

在大多数情况下,您真正​​想说的是 UNION ALL,因为它不会在集合之间进行重复消除(想想SELECT DISTINCT),这可能会节省大量执行时间。

其他人建议使用多个结果集,这是一个可行的解决方案,但我会警告在时间敏感的应用程序或通过 WAN 连接的应用程序中不要这样做,因为这样做可能会导致服务器和客户端之间的线路上的往返次数显着增加。

There are two varients of UNION.

'UNION' and 'UNION ALL'

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.

陌若浮生 2024-07-11 01:55:50

也许您应该尝试包含第四列,说明它来自的表,然后按它进行排序和分组:

SELECT A,B,C, "query 1" as origin FROM table WHERE field LIKE 'query%'
UNION
SELECT A,B,C, "query 2" as origin FROM table WHERE field LIKE '%query'
UNION
SELECT A,B,C, "query 3" as origin FROM table WHERE field LIKE '%query%'
GROUP BY origin, B ORDER BY origin, B ASC LIMIT 5

Maybe you should try including a fourth column, stating the table it came from, and then order and group by it:

SELECT A,B,C, "query 1" as origin FROM table WHERE field LIKE 'query%'
UNION
SELECT A,B,C, "query 2" as origin FROM table WHERE field LIKE '%query'
UNION
SELECT A,B,C, "query 3" as origin FROM table WHERE field LIKE '%query%'
GROUP BY origin, B ORDER BY origin, B ASC LIMIT 5
天涯离梦残月幽梦 2024-07-11 01:55:50

添加一个包含硬编码值的附加列,您将使用它对整体结果集进行排序,如下所示:

SELECT A,B,C,1 as [order] FROM table WHERE field LIKE 'query%'
UNION
SELECT A,B,C,2 as [order] FROM table WHERE field LIKE '%query'
UNION
SELECT A,B,C,3 as [order] FROM table WHERE field LIKE '%query%'
GROUP BY B ORDER BY [order] ASC, B ASC LIMIT 5

Add an additional column with hard-coded values that you will use to sort the overall resultset, like so:

SELECT A,B,C,1 as [order] FROM table WHERE field LIKE 'query%'
UNION
SELECT A,B,C,2 as [order] FROM table WHERE field LIKE '%query'
UNION
SELECT A,B,C,3 as [order] FROM table WHERE field LIKE '%query%'
GROUP BY B ORDER BY [order] ASC, B ASC LIMIT 5
时光磨忆 2024-07-11 01:55:50

你能把它作为子选择吗,比如

SELECT * FROM (
    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%'
) ORDER BY B ASC LIMIT 5

Can you do it as a subselect, something like

SELECT * FROM (
    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%'
) ORDER BY B ASC LIMIT 5
梦与时光遇 2024-07-11 01:55:50

我不明白为什么需要联合从单个表中获取数据

SELECT A, B, C 
FROM table 
WHERE field LIKE 'query%' 
   OR field LIKE '%query' 
   OR field LIKE '%query%'
GROUP BY B 
ORDER BY B ASC LIMIT 5

I don't understand why the need of union for taking the data from single table

SELECT A, B, C 
FROM table 
WHERE field LIKE 'query%' 
   OR field LIKE '%query' 
   OR field LIKE '%query%'
GROUP BY B 
ORDER BY B ASC LIMIT 5
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文