整个查询:
SELECT NULL,
ELT( MOD( A.contact_id, (SELECT count(*) FROM `ak_contact` WHERE `contact_type_id` = 12) )+1, (SELECT CONCAT( "'",GROUP_CONCAT(CAST(contact_id AS CHAR(7)) SEPARATOR "','"),"'" ) FROM `ak_contact` WHERE `contact_type_id` = 12) ) as operator_id,
B.account_id, A.contact_id, A.email
FROM ak_contact A
JOIN ak_accounts B USING (contact_id)
GROUP BY A.email, A.contact_id, B.account_id
ORDER BY A.contact_id DESC
子查询应该为每行逐个选择ID 10493,10495,11262,11800,12362,13405中的一个:
ELT( MOD( A.contact_id, (SELECT count(*) FROM `ak_contact` WHERE `contact_type_id` = 12) )+1, (SELECT CONCAT( "'",GROUP_CONCAT(CAST(contact_id AS CHAR(7)) SEPARATOR "','"),"'" )
#result: ELT(MOD(3333, 5)+1, '10493','10495','11262','11800','12362','13405')
问题是我无法强制使用子查询来处理ELT(A.contact_id,子查询)。 或者我如何制作这样的东西:
ELT(MOD(3333, 5)+1, '10493','10495','11262','11800','12362','13405')
在主查询中
答案 0 :(得分:1)
你可以使用FIND_IN_SET和一个子查询,它使用GROUP_CONCAT以逗号分隔的字符串'10493,10495,11262,...'传递所有值。
(但请注意,GROUP_CONCAT可以返回的字符串大小受服务器配置的限制。)
答案 1 :(得分:0)
MySQL 8表格,您可以使用:
WITH
cte1 AS (
SELECT
(MOD( A.contact_id, (SELECT count(*) FROM ak_contact WHERE contact_type_id = 12) )+1)mx,
B.account_id,
A.contact_id,
A.email
FROM ak_contact A
INNER JOIN ak_accounts B USING (contact_id)
GROUP BY A.email, A.contact_id, B.account_id
),
cte2 AS (
SELECT
(ROW_NUMBER() OVER ())mx,
CAST(contact_id AS CHAR(7))cid
FROM ak_contact
WHERE contact_type_id = 12
)
SELECT cte1.*, cte2.*
FROM cte1
LEFT JOIN cte2 ON cte1.mx = cte2.mx
ORDER BY cte1.contact_id DESC;