将 group_concat 的结果集限制为实际结果
limit group_concat's resultset to actual results
我有以下查询,它大部分都有效,但是 returns 在 group_concat()
中的结果太多,而其中一个加入的 table 返回了不同数量的结果:
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
我得到的结果是(请运行看片断table)或者看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 有两个不同数量的不同报价:
[[{"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
::输出::
| 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}]] |
这里有一个稍微简单的方法来完成您需要的事情。使用 GROUP_CONCAT(DISTINCT...)
.
select
a.sku, a.ek, a.mwst,
concat('[[', group_concat(DISTINCT '{"offer": ', b.offer, ', "minQuantity": ', b.minQuantity, '}') , ']]') offersA,
...
我称它为 sleazy,因为它会不正确地消除数据中确实存在的重复项。 http://sqlfiddle.com/#!9/2481e/6/0
我有以下查询,它大部分都有效,但是 returns 在 group_concat()
中的结果太多,而其中一个加入的 table 返回了不同数量的结果:
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
我得到的结果是(请运行看片断table)或者看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 有两个不同数量的不同报价:
[[{"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
::输出::
| 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}]] |
这里有一个稍微简单的方法来完成您需要的事情。使用 GROUP_CONCAT(DISTINCT...)
.
select
a.sku, a.ek, a.mwst,
concat('[[', group_concat(DISTINCT '{"offer": ', b.offer, ', "minQuantity": ', b.minQuantity, '}') , ']]') offersA,
...
我称它为 sleazy,因为它会不正确地消除数据中确实存在的重复项。 http://sqlfiddle.com/#!9/2481e/6/0