select a, REPLACE(a, ',', '') b
, LENGTH(a) x
, LENGTH(REPLACE(a, ',', '')) Y
, LENGTH(a) - LENGTH(REPLACE(a, ',', '')) + 1 z
from (select 'a,b,cc,dddd' a) t
a = ข้อความ + คอมม่า
b = ข้อความ ปราศจาก คอมม่า
x = ความยาว a
y = ความยาว b
x - y = จำนวน คอมม่า
z = จำนวน คอมม่า + 1 = จำนวน element
SELECT
x.`school_name`,
x.`class`,
x.`room`,
x.`syndrome`,
(LENGTH(x.`syndrome`) - LENGTH(REPLACE(x.`syndrome`,',','')) + 1) AS `num_rows`
FROM
(
SELECT
`school_name`,
`class`,
`room`,
GROUP_CONCAT(`syndrome` ORDER BY `id` SEPARATOR ',') AS `syndrome`
FROM
`tbl_test`
GROUP BY
`school_name`, `class`, `room`
) x
ORDER BY
x.`school_name` ASC, x.`class` ASC, x.`room` ASC;
Date :
2024-04-04 10:13:32
By :
Guest
No. 4
Guest
ตัดคำซ้ำ:
Code (SQL)
SELECT
x.`school_name`,
x.`class`,
x.`room`,
x.`syndrome`,
(LENGTH(x.`syndrome`) - LENGTH(REPLACE(x.`syndrome`,',','')) + 1) AS `num_rows`
FROM
(
SELECT
`school_name`,
`class`,
`room`,
GROUP_CONCAT(DISTINCT `syndrome` ORDER BY `id` SEPARATOR ',') AS `syndrome`
FROM
`tbl_test`
GROUP BY
`school_name`, `class`, `room`
) x
ORDER BY
x.`school_name` ASC, x.`class` ASC, x.`room` ASC;