MySQL 内连接从第二个开始限制 1 行 table

MySQL inner join limit 1 row from second table

我有 2 (MySQL) tables , exchange table exchitems 可以有 1--n 行,当 exchange 记录有多行时,我想显示单词“multi”,但是当只有 1 行时,我想显示该行的详细信息:

首先table(交换):

xid (PK) | cusid | xdate      | xref   | xtotal
1        | 1     | 2021-10-01 | 345667 | 500
2        | 1     | 2021-10-01 | 345668 | 200
3        | 1     | 2021-10-02 | 345669 | 450
4        | 1     | 2021-10-03 | 345670 | 1200

第二个table(exchitems):

chid (PK) | xid | cusid | xcur| xsell| xbuy
1         | 1   | 1     | USD | 300  | 0
2         | 1   | 1     | EUR | 0    | 400
3         | 2   | 1     | USD | 200  | 0
4         | 3   | 1     | EUR | 0    | 500
5         | 4   | 1     | EUR | 0    | 800
6         | 4   | 1     | USD | 300  | 0

exchange table 必须在 exchtiems table 中至少有 1 行,这就是我想得到:

xid | cusid | xdate      | xref   | xcur  | xsell | xbuy | xtotal
1   | 1     | 2021-10-01 | 345667 | multi | 0     | 0    | 500
2   | 1     | 2021-10-01 | 345668 | USD   | 200   | 0    | 200
3   | 1     | 2021-10-02 | 345669 | EUR   | 0     | 500  | 450
4   | 1     | 2021-10-03 | 345670 | multi | 0     | 0    | 1200

使用以下查询,我能够获取所有记录,但我想将 exchitems table 限制为一行“任何行”,当有多行时,计数用于当它大于 1 时显示单词“multi”:

SELECT a.xid,a.xdate,a.xref,a.xtotal,b.xcur,b.xsell,b.xbuy,
(SELECT COUNT(*) FROM exchitems c WHERE c.xid= a.xid) AS tRec
FROM (exchange a 
INNER JOIN exchitems b ON a.xid= b.xid AND a.cusid= b.cusid)  
WHERE a.cusid = 1 
ORDER BY a.xdate DESC,a.xid DESC 

我尝试了很多不同的查询,但无法实现我想要的。
非常感谢任何帮助。

未经测试,但这应该有效。

SELECT
    a.xid,
    a.cusid,
    a.xdate,
    a.xref,
    -- if distinct currency in the group is > 1 then the word 'multi', else currency.
    IF(COUNT(DISTINCT b.xcur) > 1, 'multi', b.xcur) AS `xcur`,
    b.xsell,
    b.xbuy,
    a.xtotal
FROM exchange a
JOIN exchitems b ON a.xid = b.xid AND a.cusid = b.cusid
WHERE a.cusid = 1
GROUP BY xid -- will let you have exchange rows with groups of exchitems 1:n
ORDER BY a.xdate DESC, a.xid DESC

您可以将当前查询修改为以下内容:

SELECT a.xid, a.cusid, a.xdate,
       a.xref,
       GROUP_CONCAT(b.xcur),
       MIN(b.xsell),
       MIN(b.xbuy),
       MAX(a.xtotal)
FROM (exchange a 
INNER JOIN exchitems b ON a.xid= b.xid AND a.cusid= b.cusid)  
WHERE a.cusid = 1 
GROUP BY a.xid,a.cusid,a.xdate,a.xref
ORDER BY a.xid;

结果将如下所示:

xid cusid xdate xref GROUP_CONCAT(b.xcur) MIN(b.xsell) MIN(b.xbuy) MAX(a.xtotal)
1 1 2021-10-01 345667 USD,EUR 0 0 500
2 1 2021-10-01 345668 USD 200 0 200
3 1 2021-10-02 345669 EUR 0 500 450
4 1 2021-10-03 345670 EUR,USD 0 0 1200

我使用MINMAX的部分是根据您的预期结果。您可能想弄清楚要显示哪个值,因为您有多个值。如果我将其更改为 GROUP_CONCAT:

 SELECT a.xid, a.cusid, a.xdate,
        a.xref,
        GROUP_CONCAT(b.xcur),
        GROUP_CONCAT(b.xsell),
        GROUP_CONCAT(b.xbuy),
        GROUP_CONCAT(a.xtotal)
 FROM (exchange a 
 INNER JOIN exchitems b ON a.xid= b.xid AND a.cusid= b.cusid)  
 WHERE a.cusid = 1 
 GROUP BY a.xid,a.cusid,a.xdate,a.xref
 ORDER BY a.xid;

然后你会看到一个更详细的结果:

xid cusid xdate xref GROUP_CONCAT(b.xcur) GROUP_CONCAT(b.xsell) GROUP_CONCAT(b.xbuy) GROUP_CONCAT(a.xtotal)
1 1 2021-10-01 345667 USD,EUR 300,0 0,400 500,500
2 1 2021-10-01 345668 USD 200 0 200
3 1 2021-10-02 345669 EUR 0 500 450
4 1 2021-10-03 345670 EUR,USD 0,300 800,0 1200,1200

要使 xcur 值显示 multi,您可能可以这样做:

SELECT a.xid, a.cusid, a.xdate,
       CASE WHEN COUNT(b.xcur) > 1 THEN 'multi' ELSE MAX(b.xcur) END AS xcur,
       MIN(b.xsell),
       MIN(b.xbuy),
       MAX(a.xtotal)
FROM (exchange a 
INNER JOIN exchitems b ON a.xid= b.xid AND a.cusid= b.cusid)  
WHERE a.cusid = 1 
GROUP BY a.xid,a.cusid,a.xdate,a.xref
ORDER BY a.xid;

Demo fiddle