【发布时间】:2021-10-08 10:04:40
【问题描述】:
我想获取列的值而不考虑表的id,因为我想使用GROUP BY来连接它们
从表中:
| id_child | id_master | test1 | test2 | test3 |
|---|---|---|---|---|
| 1 | 1 | 1 | 1 | 0 |
| 2 | 1 | 1 | 0 | 0 |
| 3 | 1 | 1 | 0 | 0 |
| 4 | 2 | 1 | 0 | 0 |
| 5 | 2 | 1 | 0 | 0 |
| 6 | 2 | 1 | 0 | 0 |
| 7 | 3 | 1 | 0 | 0 |
| 8 | 3 | 1 | 0 | 1 |
| 9 | 3 | 1 | 0 | 0 |
预期结果:
| id_master | test1 | test2 | test3 |
|---|---|---|---|
| 1 | 1 | 1 | 0 |
| 2 | 1 | 0 | 0 |
| 3 | 1 | 0 | 1 |
我的查询:
SELECT
cd.id_cutting_detail,
CD.id_cutting,
IF(cd.outermold_barcode IS NULL, '0', '1') AS `outer`,
IF(cd.midmold_barcode IS NULL, '0', '1') AS `mid`,
IF(cd.linningmold_barcode IS NULL, '0', '1') AS `linning`
FROM
cutting_detail cd
GROUP BY
cd.id_cutting
这是当前查询生成的:
| id_master | test1 | test2 | test3 |
|---|---|---|---|
| 1 | 1 | 1 | 0 |
| 2 | 1 | 0 | 0 |
| 3 | 1 | 0 | 0 |
【问题讨论】:
-
到目前为止你尝试过什么?你被困在哪里了?
-
您已经回答了自己的问题;使用
GROUP BY。请使用GROUP BY展示您的尝试,并展示它以何种具体方式不起作用。然后我们可以帮助您修复该代码。 -
@NicoHaase : 我在上面写了我的 sql 查询,得到的结果不是我想要的
-
@MatBailie:我在上面写了我的 sql 查询,得到的结果不是我想要的
-
表和查询没有任何共同点。