#1222 - 使用的SELECT语句具有不同的UNION错误列数

时间:2014-12-11 11:34:21

标签: php mysql

我花了两天多来修复这个查询,但我无法找到我的问题所在。我的问题是最后一个UNION导致了一些错误,没有它一切都很完美。 但是在最后一个UNION中,我没有不同的列数作为MYSQL错误状态。 这是查询:



INSERT INTO email_queues (queueid, queuetype, ownerid, processed, recipient)
SELECT DISTINCT 323,
                'export',
                1,
                0,
                subscriberid
FROM
  (SELECT DISTINCT subscribers.subscriberid AS subscriberid
   FROM email_list_subscribers AS subscribers
   JOIN email_lists AS lists ON lists.listid = subscribers.listid
   AND lists.listid IN (34)
   WHERE subscribers.listid IN (34)
     AND ((subscribers.emailaddress LIKE '%a%'))
   UNION SELECT DISTINCT subscribers.subscriberid AS subscriberid
   FROM email_list_subscribers AS subscribers
   JOIN email_lists AS lists ON lists.listid = subscribers.listid
   AND lists.listid IN (37)
   WHERE subscribers.listid IN (37)
     AND ((subscribers.emailaddress LIKE '%kim%'))) AS T1
WHERE subscriberid NOT IN
    (SELECT DISTINCT subscribers.subscriberid AS subscriberid
     FROM email_list_subscribers AS subscribers
     JOIN email_lists AS lists ON lists.listid = subscribers.listid
     AND lists.listid IN (37,
                          34)
     WHERE subscribers.listid IN (37,
                                  34)
       AND ((subscribers.emailaddress LIKE '%a%'
             OR subscribers.emailaddress LIKE '%kim%')))
UNION
SELECT DISTINCT subscribers.subscriberid AS subscriberid
FROM email_list_subscribers AS subscribers
JOIN email_lists AS lists ON lists.listid = subscribers.listid
AND lists.listid IN (33)
WHERE subscribers.listid IN (33)
  AND ((subscribers.emailaddress LIKE '%v%'))




1 个答案:

答案 0 :(得分:1)

您确实拥有不同数量的列。最后一个UNION列只选择了一列,其中您选择的第一个选择5如下:

INSERT INTO email_queues (queueid, queuetype, ownerid, processed, recipient)
    SELECT DISTINCT 323,                -- These are five columns
                    'export',
                    1,
                    0,
                    subscriberid
    FROM
    (
       ....
    ) AS T1
    WHERE subscriberid NOT IN (... )
    UNION
    SELECT DISTINCT subscribers.subscriberid AS subscriberid -- This where is the error, only one column selected
    FROM email_list_subscribers AS subscribers
    JOIN email_lists AS lists ON lists.listid = subscribers.listid
    AND lists.listid IN (33)
    WHERE subscribers.listid IN (33)
      AND ((subscribers.emailaddress LIKE '%v%'))

你必须像这样修改它才能使它有效:

INSERT INTO email_queues (queueid, queuetype, ownerid, processed, recipient)
SELECT DISTINCT 323,                -- These are five columns
                'export',
                1,
                0,
                subscriberid
FROM
(
   SELECT DISTINCT subscribers.subscriberid AS subscriberid
   FROM email_list_subscribers AS subscribers
   JOIN email_lists AS lists ON lists.listid = subscribers.listid AND lists.listid IN (34)
   WHERE subscribers.listid IN (34)
     AND ((subscribers.emailaddress LIKE '%a%'))
   UNION SELECT DISTINCT subscribers.subscriberid AS subscriberid
   FROM email_list_subscribers AS subscribers
   JOIN email_lists AS lists ON lists.listid = subscribers.listid AND lists.listid IN (37)
   WHERE subscribers.listid IN (37)
     AND ((subscribers.emailaddress LIKE '%kim%'))
) AS T1
WHERE subscriberid NOT IN (SELECT DISTINCT subscribers.subscriberid AS subscriberid
                           FROM email_list_subscribers AS subscribers
                           JOIN email_lists AS lists 
                             ON lists.listid = subscribers.listid  
                            AND lists.listid IN (37, 34)
                           WHERE subscribers.listid IN (37, 34)
                             AND ((subscribers.emailaddress LIKE '%a%'
                                  OR subscribers.emailaddress LIKE '%kim%'))
                          )
UNION
SELECT DISTINCT -- You have to add these in order to make the query works
  323,              
  'export',
  1,
  0,
  subscribers.subscriberid AS subscriberid -- this is only one column you selected
FROM email_list_subscribers AS subscribers
JOIN email_lists AS lists ON lists.listid = subscribers.listid
AND lists.listid IN (33)
WHERE subscribers.listid IN (33)
  AND ((subscribers.emailaddress LIKE '%v%'))

UPDATE :您也可以在子查询中移动该查询,如下所示

INSERT INTO email_queues (queueid, queuetype, ownerid, processed, recipient)
SELECT DISTINCT 323,                -- These are five columns
                'export',
                1,
                0,
                subscriberid
FROM
(
   SELECT DISTINCT subscribers.subscriberid AS subscriberid
   FROM email_list_subscribers AS subscribers
   JOIN email_lists AS lists ON lists.listid = subscribers.listid AND lists.listid IN (34)
   WHERE subscribers.listid IN (34)
     AND ((subscribers.emailaddress LIKE '%a%'))
   UNION SELECT DISTINCT subscribers.subscriberid AS subscriberid
   FROM email_list_subscribers AS subscribers
   JOIN email_lists AS lists ON lists.listid = subscribers.listid AND lists.listid IN (37)
   WHERE subscribers.listid IN (37)
     AND ((subscribers.emailaddress LIKE '%kim%'))

  UNION

  SELECT DISTINCT subscribers.subscriberid AS subscriberid -- this is only one column you selected
FROM email_list_subscribers AS subscribers
JOIN email_lists AS lists ON lists.listid = subscribers.listid
AND lists.listid IN (33)
WHERE subscribers.listid IN (33)
  AND ((subscribers.emailaddress LIKE '%v%')) 
) AS T1
WHERE subscriberid NOT IN (SELECT DISTINCT subscribers.subscriberid AS subscriberid
                           FROM email_list_subscribers AS subscribers
                           JOIN email_lists AS lists 
                             ON lists.listid = subscribers.listid  
                            AND lists.listid IN (37, 34)
                           WHERE subscribers.listid IN (37, 34)
                             AND ((subscribers.emailaddress LIKE '%a%'
                                  OR subscribers.emailaddress LIKE '%kim%'))
                          )