使用基于UNION的IN的MySQL查询失败,列数

时间:2014-08-19 11:34:39

标签: mysql

在MySQL中,我有以下语句,由于列不相同而无法运行,我不明白为什么我的where和in语句必须有相同的列。

SELECT 
  keyArchive,
  RelPath,
  ToSummary,
  Sender 
FROM
  tblarchive 
WHERE keyArchive IN 
  (SELECT 
    keyarchive 
  FROM
    tblarchive 
  WHERE (
      MATCH(SenderEmail) AGAINST ('"name@company.com"')
    )) 
  UNION
  (SELECT 
    keyfarchive 
  FROM
    tblarchiverecipients 
  WHERE recipientemail = 'name@company.com')

如果我在keyarchive in之后运行语句,它就会正确完成。

1 个答案:

答案 0 :(得分:1)

你正试图结合最初的

SELECT 
  keyArchive,
  RelPath,
  ToSummary,
  Sender 

使用最终的SELECT查询进行查询。联合必须具有相同数量的列。我怀疑你打算将两个后面的查询联合起来。尝试:

SELECT 
  keyArchive,
  RelPath,
  ToSummary,
  Sender 
FROM
  tblarchive 
WHERE keyArchive IN 
  ((SELECT 
    keyarchive 
  FROM
    tblarchive 
  WHERE (
      MATCH(SenderEmail) AGAINST ('"name@company.com"')
    )) 
  UNION
  (SELECT 
    keyfarchive 
  FROM
    tblarchiverecipients 
  WHERE recipientemail = 'name@company.com'))

唯一的补充是将IN子句包装在括号中。