麻烦结合三个SELECT语句,UNION无法正常工作

时间:2015-10-02 10:07:48

标签: mysql sql database select union

我在组合三个select语句时遇到了很大的麻烦,我尝试过联合它不起作用,因为它们可以使用单独的select语句但是我需要其余的是一个快速语句,如果可能的话甚至可以随机输入最终记录为了

      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

2 个答案:

答案 0 :(得分:0)

如果您在每个查询中都需要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

答案 1 :(得分:0)

为了在部分查询而不是整体上使用ORDER BY子句,请使用括号:

(
 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
)