【问题标题】:how to query get value without id如何查询没有id的值
【发布时间】: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 查询,得到的结果不是我想要的
  • 表和查询没有任何共同点。

标签: mysql sql database


【解决方案1】:
SELECT
  id_cutting,
  `outer`, 
  `mid`,
  `lining`
(
  SELECT
    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
)
  AS nulls_checked
GROUP BY
  id_cutting,
  `outer`, 
  `mid`,
  `lining`

或者可能……

  SELECT
    cd.id_cutting,
    MAX(IF(cd.outermold_barcode IS NULL, '0', '1')) AS `outer`,
    MAX(IF(cd.midmold_barcode IS NULL, '0', '1')) AS `mid`,
    MAX(IF(cd.linningmold_barcode IS NULL, '0', '1') ) AS `linning`
  FROM
    cutting_detail cd
  GROUP BY
    cd.id_cutting

【讨论】:

    【解决方案2】:
    SELECT id_master
         , MAX(test1) test1
         , MAX(test2) test2
         , MAX(test3) test3
      FROM the_table
     GROUP
        BY id_master
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-07
      • 2017-01-12
      • 2016-04-14
      • 2019-01-28
      • 2010-11-28
      • 1970-01-01
      相关资源
      最近更新 更多