我有这个问题:
SELECT GUITAR_NO,COUNT(INVENTORY)
FROM INVENTORY.GUITARS
WHERE guitars.guitar_type = 'FENDER' and GUITARS.Guitar_no LIKE('4%')
GROUP BY GUITAR_NO
我试图让查询只返回6个字符长的吉他字符,不再短。有没有办法在DB2中执行此操作?
奖励:我如何仅返回那些吉他_no,其中Count(库存)> 3
答案 0 :(得分:1)
只需使用LENGTH
函数返回传入的字段的整数表示。
在您的WHERE
条款中,只需添加AND LENGTH(GuitarName) = 6
,只需将GuitarName
替换为您需要检查的字段。
除去额外的空格,只需将其包裹在TRIM
函数
LENGTH(TRIM(GuitarName)) = 6
加成:
只需在HAVING
GROUP BY
条款即可
SELECT GUITAR_NO,COUNT(INVENTORY)
FROM INVENTORY.GUITARS
WHERE guitars.guitar_type = 'FENDER'
and GUITARS.Guitar_no LIKE('4%')
and LENGTH(TRIM(guitars.guitar_type)) = 6
GROUP BY GUITAR_NO
HAVING COUNT(INVENTORY) > 3
答案 1 :(得分:1)
在LENGTH(TRIM(guitar_type)) = 6
条款
WHERE
在HAVING COUNT(INVENTORY) > 3
条款
GROUP BY
醇>