这是我的表“资产”
+----+----------------------------+-------+-----------+
| id | model | grade | warehouse |
+----+----------------------------+-------+-----------+
| 1 | Optiplex 9030 AIO i5 | 2 | 2 |
| 2 | Optiplex 9030 AIO i3 Touch | 2 | 2 |
| 3 | Optiplex 9030 AIO i7 | 2 | 1 |
| 4 | Optiplex 9030 AIO i5 Touch | 1 | 1 |
| 5 | Optiplex 9030 AIO i7 | 1 | 1 |
| 6 | Optiplex 9030 AIO i7 | 2 | 1 |
| 7 | Optiplex 9030 AIO i7 | 1 | 2 |
| 8 | Optiplex 9030 AIO i5 Touch | 2 | 2 |
| 9 | Optiplex 9030 AIO i5 | 1 | 1 |
| 10 | Optiplex 9030 AIO i5 | 2 | 2 |
+----+----------------------------+-------+-----------+
我想做的是
SELECT id, model, grade, @ids := GROUP_CONCAT(id) AS ids,
(SELECT COUNT(*) FROM assets WHERE id IN (@ids) AND warehouse = 1 ) AS w_1_count,
(SELECT COUNT(*) FROM assets WHERE id IN (@ids) AND warehouse = 2 ) AS w_2_count
FROM `assets` GROUP BY model, grade
它给我这样的结果
+----+----------------------------+-------+------+-----------+-----------+
| id | model | grade | ids | w_1_count | w_2_count |
+----+----------------------------+-------+------+-----------+-----------+
| 2 | Optiplex 9030 AIO i3 Touch | 2 | 2 | 1 | 0 |
| 9 | Optiplex 9030 AIO i5 | 1 | 9 | 0 | 1 |
| 1 | Optiplex 9030 AIO i5 | 2 | 1,10 | 1 | 0 |
| 4 | Optiplex 9030 AIO i5 Touch | 1 | 4 | 0 | 1 |
| 8 | Optiplex 9030 AIO i5 Touch | 2 | 8 | 1 | 0 |
| 5 | Optiplex 9030 AIO i7 | 1 | 5,7 | 0 | 1 |
| 3 | Optiplex 9030 AIO i7 | 2 | 3,6 | 1 | 0 |
+----+----------------------------+-------+------+-----------+-----------+
预期的结果是:我想得到哪个?但没有运气
+----+----------------------------+-------+------+-----------+-----------+
| id | model | grade | ids | w_1_count | w_2_count |
+----+----------------------------+-------+------+-----------+-----------+
| 2 | Optiplex 9030 AIO i3 Touch | 2 | 2 | 0 | 1 |
| 9 | Optiplex 9030 AIO i5 | 1 | 9 | 1 | 0 |
| 1 | Optiplex 9030 AIO i5 | 2 | 1,10 | 0 | 2 |
| 4 | Optiplex 9030 AIO i5 Touch | 1 | 4 | 1 | 0 |
| 8 | Optiplex 9030 AIO i5 Touch | 2 | 8 | 0 | 1 |
| 5 | Optiplex 9030 AIO i7 | 1 | 5,7 | 1 | 1 |
| 3 | Optiplex 9030 AIO i7 | 2 | 3,6 | 2 | 0 |
+----+----------------------------+-------+------+-----------+-----------+
当我选择 @ids 变量进行显示时,它给了我奇怪的结果集
SELECT id, model, grade, @ids := GROUP_CONCAT(id) AS ids, @ids,
(SELECT COUNT(*) FROM assets WHERE id IN (@ids) AND warehouse = 1 ) AS w_1_count,
(SELECT COUNT(*) FROM assets WHERE id IN (@ids) AND warehouse = 2 ) AS w_2_count
FROM `assets` GROUP BY model, grade
+----+----------------------------+-------+------+------+-----------+-----------+
| id | model | grade | ids | @ids | w_1_count | w_2_count |
+----+----------------------------+-------+------+------+-----------+-----------+
| 2 | Optiplex 9030 AIO i3 Touch | 2 | 2 | 3,6 | 1 | 0 |
| 9 | Optiplex 9030 AIO i5 | 1 | 9 | 2 | 0 | 1 |
| 1 | Optiplex 9030 AIO i5 | 2 | 1,10 | 9 | 1 | 0 |
| 4 | Optiplex 9030 AIO i5 Touch | 1 | 4 | 1,10 | 0 | 1 |
| 8 | Optiplex 9030 AIO i5 Touch | 2 | 8 | 4 | 1 | 0 |
| 5 | Optiplex 9030 AIO i7 | 1 | 5,7 | 8 | 0 | 1 |
| 3 | Optiplex 9030 AIO i7 | 2 | 3,6 | 5,7 | 1 | 0 |
+----+----------------------------+-------+------+------+-----------+-----------+
问题:
1.) 如何实现与@ids 和另一个条件绑定的预期结果集?
2.) 为什么@ids 列显示下一行的错误值,它应该与group_concat ids 相同?我不知道我错在哪里
在这里,我附上了一个 SQL fiddle 以获得快速帮助