简体   繁体   English

如何在同一个表上合并两个查询以在MySQL中获得单个结果集

[英]How do I combine two queries on the same table to get a single result set in MySQL

I am not very good at sql but I am getting there. 我不太擅长sql,但我正在实现。 I have searched stackoverflow but I can't seem to find the solution and I hope someone out there can help me. 我已经搜索了stackoverflow,但是似乎找不到解决方案,希望有人可以帮助我。 I have a table (users) with data like the following. 我有一个表(用户),其中包含如下数据。 The book_id column is a key to another table that contains a book the user is subscribed to. book_id列是另一个表的键,该表包含用户已预订的书。

|--------|---------------------|------------------|
|   id   |      book_id        |       name       |
|--------|---------------------|------------------|
|   1    |         1           |        jim       |
|   2    |         1           |       joyce      |
|   3    |         1           |        mike      |
|   4    |         1           |       eleven     |
|   5    |         2           |        max       |
|   6    |         2           |       dustin     |
|   7    |         2           |       lucas      |
|--------|---------------------|------------------|

I have a function in my PHP code that returns two random users from a specific book id (either 1 or 2). 我的PHP代码中有一个函数,可以从特定的书号(1或2)返回两个随机用户。 Query one returns the result in column 1 and result two returns the results in column 2 like: 查询一返回第1列的结果,结果二返回第2列的结果,例如:

|---------------------|------------------|
|          1          |        2         |
|---------------------|------------------|
|        jim          |       max        |
|       joyce         |     dustin       |
|---------------------|------------------|

I have achieved this by running two separate queries as seen below. 我通过运行两个独立的查询来实现这一点,如下所示。 I want to know if it's possible to achieve this functionality with one query and how. 我想知道是否可以通过一个查询以及如何实现此功能。

$random_users_with_book_id_1 = SELECT name FROM users WHERE book_id=1 LIMIT 2
$random_users_with_book_id_2 = SELECT name FROM users WHERE book_id=2 LIMIT 2

Again, I apologise if it's too specific. 再说一次,我很抱歉。 The query below has been closest to what I was trying to achieve.: 下面的查询与我试图实现的最接近:

SELECT a.name AS book_id_1, b.name AS book_id_2   
FROM users a, users b 
WHERE a.book_id=1 AND b.book_id = 2
LIMIT 2

EDIT: I have created a fiddle to play around with his. 编辑:我创建了一个小提琴与他一起玩。 I appreciate any help! 感谢您的帮助! Thank you!! 谢谢!! http://sqlfiddle.com/#!9/7fcbca/1 http://sqlfiddle.com/#!9/7fcbca/1

It is easy actually :) 实际上很容易:)

you can use UNION like this: 您可以这样使用UNION:

SELECT * FROM (
     (SELECT * FROM user WHERE n_id=1 LIMIT 2)
     UNION
     (SELECT * FROM user WHERE n_id=2 LIMIT 2)) 
 collection;

if you read this article about the documentation you can use the () to group the individual queries and the apply the union in the middle. 如果您阅读有关文档的本文, 可以使用()对单个查询进行分组,并在中间应用并集。 Without the parenthesis it would still LIMIT 2 and show only the two first. 如果没有括号,它将仍然是LIMIT 2,并且仅显示前两个。 Ref. 参考。 "To apply ORDER BY or LIMIT to an individual SELECT, place the clause inside the parentheses that enclose the SELECT:" “要将ORDER BY或LIMIT应用于单个SELECT,请将子句放在包围SELECT的括号内:”

If you want to combine the queries in MySQL, you can just use parentheses: 如果要在MySQL中合并查询,则可以使用括号:

(SELECT name
 FROM users
 WHERE n_id = 1
 LIMIT 2
) UNION ALL
(SELECT name
 FROM users
 WHERE n_id = 2
 LIMIT 2
);

First, only use UNION if you specifically want to incur the overhead of removing duplicates. 首先,仅在您特别想承担删除重复项的开销时才使用UNION Otherwise, use UNION ALL . 否则,请使用UNION ALL

Second, this does not return random rows. 其次,这不会返回随机行。 This returns arbitrary rows. 这将返回任意行。 In many cases, this might be two rows near the beginning of the data. 在许多情况下,这可能是数据开头附近的两行。 If you want random rows, then use ORDER BY rand() : 如果要随机行,请使用ORDER BY rand()

(SELECT name
 FROM users
 WHERE n_id = 1
 ORDER by rand()
 LIMIT 2
) UNION ALL
(SELECT name
 FROM users
 WHERE n_id = 2
 ORDER BY rand()
 LIMIT 2
);

There are other methods that are more efficient, but this should be fine for up to a few thousand rows. 还有其他更有效的方法,但是对于多达几千行来说应该没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM