SELECT COUNT(*) AS count
FROM (
SELECT id, b_v, GROUP_CONCAT(pos ORDER BY pos) AS traits
FROM (
SELECT id, b_v, pos
FROM (
SELECT id, b_v, LENGTH(b_v) - n + 1 AS pos, SUBSTRING(b_v, n, 1) AS bit_value
FROM (
SELECT id, BIN(GENOTYPE) AS b_v, t.n
FROM ECOLI_DATA
CROSS JOIN (
SELECT 1 AS n UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4
UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8
UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12
UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL SELECT 15 UNION ALL SELECT 16
) t
WHERE t.n <= LENGTH(BIN(GENOTYPE))
) AS binary_positions
) AS extracted_positions
WHERE bit_value = '1'
) AS pos_query
GROUP BY id, b_v
HAVING FIND_IN_SET(2, traits) = 0 AND (FIND_IN_SET(1, traits) > 0 OR FIND_IN_SET(3, traits) > 0)
) AS filtered_data;
CROSS JOIN (
SELECT 1 AS n UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4
UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8
UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12
UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL SELECT 15 UNION ALL SELECT 16
) t
SELECT id, b_v, GROUP_CONCAT(pos ORDER BY pos) AS traits
GROUP_CONCAT(pos ORDER BY pos)
order by 안해도 실은 자동으로 오름차순 화해서 나옴.
GROUP BY id, b_v
HAVING FIND_IN_SET(2, traits) = 0 AND (FIND_IN_SET(1, traits) > 0 OR FIND_IN_SET(3, traits) > 0)
) AS filtered_data;
즉,
traits 에서 "2"는 제외 (조건1)
AND
traits에서 "3" OR "1"이 포함됬는지 (조건2).
발견하지 못했을때 return 0;
syntax: FIND_IN_SET(value, string_list)