mysql – 将group_concat的结果集限制为实际结果
作者:互联网
我有以下查询主要工作,但当其中一个连接表返回的结果数不同时,在group_concat()中返回太多结果:
select
a.sku, a.ek, a.mwst,
concat('[[', group_concat('{"offer": ', b.offer, ', "minQuantity": ', b.minQuantity, '}') , ']]') offersA,
concat('[[', group_concat('{"offer": ', c.offer, ', "minQuantity": ', c.minQuantity, '}') , ']]') offersB,
concat('[[', group_concat('{"offer": ', d.offer, ', "minQuantity": ', d.minQuantity, '}') , ']]') offersC
from all_prices a
left join all_prices_a b on a.sku = b.sku
left join all_prices_b c on a.sku = c.sku
left join all_prices_c d on a.sku = d.sku
where a.sku in (123,456)
group by a.sku
我得到的结果是(请运行代码片段查看表格)或查看fiddle
<table border=1>
<tr>
<td bgcolor=silver class='medium'>sku</td>
<td bgcolor=silver class='medium'>ek</td>
<td bgcolor=silver class='medium'>mwst</td>
<td bgcolor=silver class='medium'>offersA</td>
<td bgcolor=silver class='medium'>offersB</td>
<td bgcolor=silver class='medium'>offersC</td>
</tr>
<tr>
<td class='normal' valign='top'>123</td>
<td class='normal' valign='top'>154.32</td>
<td class='normal' valign='top'>19</td>
<td class='normal' valign='top'>[[{"offer": 9.65, "minQuantity": 3},{"offer": 9.86, "minQuantity": 1}]]</td>
<td class='normal' valign='top'>[[{"offer": 9.66, "minQuantity": 1},{"offer": 9.66, "minQuantity": 1}]]</td>
<td class='normal' valign='top'>[[{"offer": 9.65, "minQuantity": 1},{"offer": 9.65, "minQuantity": 1}]]</td>
</tr>
<tr>
<td class='normal' valign='top'>456</td>
<td class='normal' valign='top'>48.48</td>
<td class='normal' valign='top'>19</td>
<td class='normal' valign='top'>[[{"offer": 13.30, "minQuantity": 1},{"offer": 13.30, "minQuantity": 1}]]</td>
<td class='normal' valign='top'>[[{"offer": 13.30, "minQuantity": 1},{"offer": 122.00, "minQuantity": 3}]]</td>
<td class='normal' valign='top'>NULL</td>
</tr>
</table>
如您所见,例如offersB包含两个结果
[[{"offer": 9.66, "minQuantity": 1},{"offer": 9.66, "minQuantity": 1}]]
如果两者都相等,则数据库中只有一个条目用于给定的sku 123,但offersA对此sku的不同数量有两个不同的要约:
[[{"offer": 9.65, "minQuantity": 3},{"offer": 9.86, "minQuantity": 1}]]
我以后使用JavaScript处理结果,所以我可以删除重复的结果 – 但我想知道是否存在
a)一种更聪明的查询数据的方法
b)一种在查询本身中删除这些重复项的方法
解决方法:
试试这个:
SELECT a.sku, a.ek, a.mwst,
CONCAT('[[', b.offersA , ']]') offersA,
CONCAT('[[', c.offersB , ']]') offersB,
CONCAT('[[', d.offersC , ']]') offersC
FROM all_prices a
LEFT JOIN ( SELECT b.sku, GROUP_CONCAT('{"offer": ', b.offer, ', "minQuantity": ', b.minQuantity, '}') AS offersA
FROM all_prices_a b
GROUP BY b.sku
) AS b ON a.sku = b.sku
LEFT JOIN ( SELECT c.sku, GROUP_CONCAT('{"offer": ', c.offer, ', "minQuantity": ', c.minQuantity, '}') AS offersB
FROM all_prices_b c
GROUP BY c.sku
) AS c ON a.sku = c.sku
LEFT JOIN ( SELECT d.sku, GROUP_CONCAT('{"offer": ', d.offer, ', "minQuantity": ', d.minQuantity, '}') AS offersC
FROM all_prices_c d
GROUP BY d.sku
) AS d ON a.sku = d.sku
WHERE a.sku IN (123, 456);
检查这个SQL FIDDLE DEMO
:: OUTPUT ::
| sku | ek | mwst | offersA | offersB | offersC |
|-----|-------|------|---------------------------------------------------------------------------|----------------------------------------|----------------------------------------|
| 123 | 12.48 | 19 | [[{"offer": 12.28, "minQuantity": 1},{"offer": 11.24, "minQuantity": 3}]] | [[{"offer": 12.28, "minQuantity": 1}]] | [[{"offer": 12.28, "minQuantity": 1}]] |
| 456 | 13.24 | 19 | [[{"offer": 10.00, "minQuantity": 1},{"offer": 9.00, "minQuantity": 3}]] | [[{"offer": 9.00, "minQuantity": 3}]] | [[{"offer": 9.00, "minQuantity": 3}]] |
标签:sql,mysql,join,select,group-concat 来源: https://codeday.me/bug/20190829/1761386.html