我有以下查询主要工作,但当其中一个连接表返回的结果数不同时,在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>
&#13;
如您所见,例如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)在查询本身中删除这些重复项的方法
答案 0 :(得分:4)
试试这个:
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);
<强> :: 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}]] |
答案 1 :(得分:1)
这是一种稍微轻松的方式来做你需要的。使用GROUP_CONCAT(DISTINCT...)
。
select
a.sku, a.ek, a.mwst,
concat('[[', group_concat(DISTINCT '{"offer": ', b.offer, ', "minQuantity": ', b.minQuantity, '}') , ']]') offersA,
...
我称之为肮脏,因为它会不正确地消除数据中真正存在的重复项。 http://sqlfiddle.com/#!9/2481e/6/0