CREATE TABLE语句:
CREATE TABLE `Tovar` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`idPreparat` MEDIUMINT(6) UNSIGNED NULL DEFAULT NULL,
`drugApt` VARCHAR(250) NULL DEFAULT NULL,
`countryApt` VARCHAR(250) NULL DEFAULT NULL,
`manfApt` VARCHAR(250) NULL DEFAULT NULL,
`md5` BINARY(16) NOT NULL,
PRIMARY KEY (`id`),
INDEX `ix_prep` (`idPreparat`),
INDEX `ix_md5` (`md5`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
;
查询:
SELECT id,
IF(COUNT(Tovar.id)=1,Tovar.drugApt,'') AS `drugApt`,
IF(COUNT(Tovar.id)=1,Tovar.countryApt,'') AS `countryApt`,
IF(COUNT(Tovar.id)=1,Tovar.manfApt,'') AS `manfApt`
FROM Tovar
WHERE Tovar.md5=0x00000000000000000000000000000000
GROUP BY Tovar.idPreparat
如何在一个IF上替换三个IF(COUNT(Tovar.id)= 1 ,,)? 添加我的想法:
CASE COUNT(Tovar.id)>1
WHEN true THEN CONCAT_WS(@drugApt:='Tovar.drugApt',@countryApt='Tovar.countryApt',@manfApt='Tovar.manfApt')
WHEN false THEN CONCAT_WS(@drugApt:='',@countryApt='',@manfApt='')
END
答案 0 :(得分:0)
如果我正确理解您的请求,则子查询可能有助于实现所需的结果:
SELECT tmp.id,
COALESCE(t.drugApt, '') as drugApt,
COALESCE(t.countryApt, '') as countryApt,
COALESCE(t.manfApt, '') as manfApt
FROM (
SELECT id,
IF(COUNT(id)=1, 1, 0) as cond
FROM Tovar
WHERE md5 = 0x00000000000000000000000000000000
GROUP BY idPreparat
) tmp
LEFT JOIN Tovar t
ON t.id = tmp.id
AND tmp.cond = 1
这有帮助吗?