MySQL对第二次连接的限制

时间:2019-04-22 18:07:47

标签: mysql join limit

我不确定如何限制查询中的第二个联接。在我准备的fiddle中,我需要限制结果中每个用户的行数。

表a:

id | word
----------
1  | xyz
2  | zzz

表b:

name  | word
------------
Peter | xyz
John  | xyz
Jane  | xyz

表c:

name  | product
------------
Peter | blah
Peter | blah2
Peter | blah3
Peter | blah4
Peter | blah5
Peter | blah6
John | hello
John | world
John | blah

所需结果:表b的前两个条目(用户Peter和John),以及每个用户在表c上的前两个条目。

name  | word | product
----------------------
Peter | xyz | blah
Peter | xyz | blah2
John  | xyz | hello
John  | xyz | world

我当前的查询是

select c.name, first_join.word, c.product from (
select b.* from a
left join b on b.word=a.word
where a.id=1
limit 2
) first_join
left join c on c.name=first_join.name

它产生结果:

name  | word | product
------------------------------
Peter | xyz  | blah
Peter | xyz  | blah2
Peter | xyz  | blah3
Peter | xyz  | blah4
Peter | xyz  | blah5
Peter | xyz  | blah6
John  | xyz  | hello
John  | xyz  | world
John  | xyz  | blah

您有什么想法吗?我找到了另一个讨论此问题的线程,但无法将其映射到我的情况。我知道结构并不完美,但不允许更改表c的设计。

2 个答案:

答案 0 :(得分:0)

让我们剖析您的查询:

SELECT b.* FROM a
LEFT JOIN b ON b.word=a.word
WHERE a.id=1
LIMIT 2
-- This sub-query returns the following result:
+-------+------+
| name  | word |
+-------+------+
| Peter | xyz  |
| John  | xyz  |
+-------+------+

如果没有子查询的完整语法,则您的主要语法如下:

SELECT c.name, first_join.word, c.product FROM first_join
LEFT JOIN c ON c.name=first_join.name;

让我们将c.name, first_join.word, c.product替换为*。然后您将得到如下结果:

+-------+------+-------+---------+
| name  | word | name  | product |
+-------+------+-------+---------+
| Peter | xyz  | Peter | blah    |
| Peter | xyz  | Peter | blah2   |
| Peter | xyz  | Peter | blah3   |
| Peter | xyz  | Peter | blah4   |
| Peter | xyz  | Peter | blah5   |
| Peter | xyz  | Peter | blah6   |
| John  | xyz  | John  | hello   |
| John  | xyz  | John  | world   |
| John  | xyz  | John  | blah    |
+-------+------+-------+---------+

现在,您在这里问自己这是否是您想要的?如果不是,则需要确保您了解此处要执行的操作的逻辑。例如,为什么您的first_join子查询返回两行,但是当您LEFT JOIN时它返回table c的所有行? 好吧,答案是因为您的ON c.name=first_join.name。它与table c中的所有peter和john匹配,无论您在子查询中施加的限制如何。如果对外部查询设置了限制,那么它将无法正常工作。

tl;博士 如果您使用的是MySQL 8.0,则可以在下面尝试以下查询:

SELECT name,word,product FROM
(SELECT 
    b.name, b.word,c.product,
    ROW_NUMBER() OVER (PARTITION BY c.name ORDER BY c.name DESC) AS wRow
FROM 
a JOIN
b ON a.word=b.word JOIN
c ON c.name=b.name
WHERE a.id=1) r
WHERE wRow IN (1,2)
ORDER BY name DESC;

Fiddle

另外两个示例:

-- First: by using INNER JOIN and UNION (as suggested in the comment).
SELECT b.name,b.word,r.product FROM a JOIN b ON a.word=b.word JOIN
((SELECT * FROM c WHERE c.name='peter' LIMIT 2) UNION
(SELECT * FROM c WHERE c.name='john' LIMIT 2)) r
ON b.name=r.name 
WHERE a.id=1;

-- Second: by using LEFT JOIN.    
SELECT b.Name,b.Word,IF(p.product IS NULL,j.product,p.product) AS 'Product' 
FROM a JOIN b ON a.word=b.word LEFT JOIN
(SELECT * FROM c WHERE NAME='peter' LIMIT 2) p ON b.name=p.name LEFT JOIN
(SELECT * FROM c WHERE NAME='john' LIMIT 2) j ON b.name=j.name
WHERE a.id=1 AND b.name IN (p.name,j.name);

更新:好的。不漂亮,但此查询可以工作。我已经在MySQL 5.7上进行了测试。 ;)

SELECT b.Name,b.Word,c.Product FROM a 
JOIN b ON a.word=b.word 
JOIN c ON b.name=c.name 
JOIN (SELECT c.name,
REPLACE(SUBSTRING_INDEX(GROUP_CONCAT(product SEPARATOR ' '),' ',2),' ',',') prod 
FROM c GROUP BY NAME) r
ON b.name=r.name WHERE FIND_IN_SET(c.product,prod);

但是,如果您的列至少具有自动增量值,则会容易得多。

答案 1 :(得分:0)

您可以使用存储过程,选择不同的名称,循环搜索结果,然后为每个名称选择2行。

以下存储过程为您提供了所需的结果(这在MySQL 5.6服务器上有效,但在sqlfiddle中不可用):

drop procedure if exists fetch_names;

DELIMITER $$

CREATE PROCEDURE fetch_names()

    BEGIN 
    DECLARE v_ready INTEGER DEFAULT 0;
    DECLARE v_name varchar(100) DEFAULT "";
    -- first fetch all distinct names
    DECLARE names_cursor CURSOR FOR select distinct c.name from c;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_ready = 1;

    -- use a temporary table to store results
    CREATE TEMPORARY TABLE names_temp ( `name` varchar(255),  `word` varchar(255),  `product` varchar(255));

    OPEN names_cursor;

    -- loop through all names
    get_names: LOOP
    FETCH names_cursor INTO v_name;

    IF v_ready = 1 THEN 
     LEAVE get_names;
     END IF;

    -- get 2 results for each name (you may add some order parameter here)
    INSERT INTO names_temp SELECT c.name, b.word, c.product from a
    LEFT JOIN b on a.word = b.word
    LEFT JOIN c on b.name = c.name
    WHERE c.name = v_name
    LIMIT 2;

    END LOOP get_names;
    CLOSE names_cursor;

    -- output the collected result and drop the temporary table
    SELECT * FROM names_temp;
    DROP TEMPORARY TABLE IF EXISTS names_temp;

END$$
DELIMITER ;

-- call the procedure and get the results
call fetch_names();