我有这张桌子
------------------------------------------
id | Machines
------------------------------------------
1. | 1 Truck
------------------------------------------
2. | 1 Bobcat
------------------------------------------
3. | 2 Platform
------------------------------------------
4. | Telehender
------------------------------------------
5. | Teodolit, 3 Platform
------------------------------------------
6. | 2 Tractor
------------------------------------------
7. | NULL
------------------------------------------
Result: | 11
------------------------------------------
我想先对整数(1 + 1 + 2 + 3 + 2)求和,然后对不带整数的值求和(Telehender和Teodolit = 2),然后跳过NULL值。 该表的结果必须为11。
我正在使用此查询
SELECT Sum((Char_length(machines) - Char_length(Replace(machines, ',','')) + 1)) AS ukupno
FROM izvestaji
WHERE projekatid='8'
AND datum='2019-10-03'
但是我得到的结果是9。
有没有办法进行查询?
答案 0 :(得分:1)
错误的数据库设计...:/ 但是,在这里它是一个解决方案,其中我假设您不超过3个元素 进入机器领域
SELECT
sum(case
when length(machines)-length(replace(machines,',','')) = 0
then if( CAST(machines AS UNSIGNED) = 0, 1, cast(machines as unsigned))
when length(machines)-length(replace(machines,',','')) = 1
then ( if( CAST(SUBSTRING_INDEX(machines, ',', 1) AS UNSIGNED) = 0, 1, cast(SUBSTRING_INDEX(machines, ',', 1) as unsigned))) +
(if( CAST(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 1),','),'') AS UNSIGNED) = 0, 1, cast(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 1),','),'') as unsigned)))
when length(machines)-length(replace(machines,',','')) = 2
then ( if( CAST(SUBSTRING_INDEX(machines, ',', 1) AS UNSIGNED) = 0, 1, cast(SUBSTRING_INDEX(machines, ',', 1) as unsigned))) +
(if( CAST(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 1),','),'') AS UNSIGNED) = 0, 1, cast(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 1),','),'') as unsigned))) +
(if( CAST(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 2),','),'') AS UNSIGNED) = 0, 1, cast(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 2),','),'') as unsigned)))
end) AS ukupno
FROM izvestaji
#WHERE projekatid='8'
#AND datum='2019-10-03'
您可以获得2到M的其他情况,包括类似情况
when length(machines)-length(replace(machines,',','')) = M
then ( if( CAST(SUBSTRING_INDEX(machines, ',', 1) AS UNSIGNED) = 0, 1, cast(SUBSTRING_INDEX(machines, ',', 1) as unsigned))) +
(if( CAST(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 1),','),'') AS UNSIGNED) = 0, 1, cast(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 1),','),'') as unsigned))) +
(if( CAST(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 2),','),'') AS UNSIGNED) = 0, 1, cast(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 2),','),'') as unsigned))) +
...
(if( CAST(replace(machines , concat(SUBSTRING_INDEX(machines, ',', j),','),'') AS UNSIGNED) = 0, 1, cast(replace(machines , concat(SUBSTRING_INDEX(machines, ',', j),','),'') as unsigned))) +
...
(if( CAST(replace(machines , concat(SUBSTRING_INDEX(machines, ',', M),','),'') AS UNSIGNED) = 0, 1, cast(replace(machines , concat(SUBSTRING_INDEX(machines, ',', M),','),'') as unsigned)))
如果您看到案例1或案例2,我认为您可以理解。
答案 1 :(得分:1)