gpt4 book ai didi

mysql - 组合三个 SELECT 语句时出现问题,UNION 不起作用

转载 作者:行者123 更新时间:2023-11-29 21:58:23 25 4
gpt4 key购买 nike

我在组合三个选择语句时遇到了巨大的麻烦,我尝试过联合它不起作用,因为单独的选择语句它们确实有效,但是我需要其余的成为一个快速语句,如果可能的话,甚至将最终记录随机放入订单

      SELECT DISTINCT email 
FROM customer_1_tbl
WHERE email NOT IN (SELECT temp.email
FROM temp_emails AS temp)
AND substring_index(email, '@', -1) = 'seznam.cz'
ORDER BY RAND()
LIMIT 6

UNION

SELECT DISTINCT email
FROM customer_1_tbl
WHERE email NOT IN (SELECT temp.email
FROM temp_emails AS temp)
AND substring_index(email, '@', -1) = 'gmail.com'
ORDER BY RAND()
LIMIT 6

UNION

SELECT DISTINCT email
FROM customer_1_tbl
WHERE email NOT IN (SELECT temp.email
FROM temp_emails AS temp)
AND substring_index(email, '@', -1) != 'gmail.com'
AND substring_index(email, '@', -1) != 'seznam.cz'
AND RAND() < (SELECT ((6/COUNT(*))*10)
FROM customer_1_tbl)
ORDER BY RAND()
LIMIT 6

最佳答案

如果您在每个查询中都需要 ORDER BY,请尝试以下操作:

SELECT * FROM(
SELECT DISTINCT email
FROM customer_1_tbl
WHERE email NOT IN (SELECT temp.email
FROM temp_emails AS temp)
AND substring_index(email, '@', -1) = 'seznam.cz'
ORDER BY RAND()
LIMIT 6
) AS A

UNION

SELECT * FROM (
SELECT DISTINCT email
FROM customer_1_tbl
WHERE email NOT IN (SELECT temp.email
FROM temp_emails AS temp)
AND substring_index(email, '@', -1) = 'gmail.com'
ORDER BY RAND()
LIMIT 6
) AS B

UNION
SELECT * FROM (
SELECT DISTINCT email
FROM customer_1_tbl
WHERE email NOT IN (SELECT temp.email
FROM temp_emails AS temp)
AND substring_index(email, '@', -1) != 'gmail.com'
AND substring_index(email, '@', -1) != 'seznam.cz'
AND RAND() < (SELECT ((6/COUNT(*))*10)
FROM customer_1_tbl)
ORDER BY RAND()
LIMIT 6
) AS C

关于mysql - 组合三个 SELECT 语句时出现问题,UNION 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32904953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com