【问题标题】:MySQL error: Operand should contain 1 column(s)MySQL 错误:操作数应包含 1 列
【发布时间】:2012-12-18 16:42:40
【问题描述】:

我有这个 MySQL 查询(工作)

第一个查询:

SELECT id
FROM users
WHERE publisher_set = '1'
AND publisher_status = '1'
AND publisher_content != ''
AND publisher_amount != '0'
AND publisher_now < publisher_max
AND EXISTS (

SELECT *
FROM user_counter
WHERE users.id = user_counter.publisher_id
)

上面的MySQL查询是从两个表中查找用户id

现在我想再次使用第二个 MySQL 查询进行比较(工作)

第二次查询:

SELECT users.id, publisher_amount, publisher_now, publisher_max, counter
FROM users
INNER JOIN user_counter ON users.id = user_counter.publisher_id
WHERE no = 08123456789
AND counter < publisher_amount

但是当我像这样加入所有查询时:

SELECT id
FROM users
WHERE publisher_set = '1'
AND publisher_status = '1'
AND publisher_content != ''
AND publisher_amount != '0'
AND publisher_now < publisher_max
AND EXISTS (

SELECT *
FROM user_counter
WHERE users.id = user_counter.publisher_id
)

AND (
SELECT users.id, publisher_amount, publisher_now, publisher_max, counter
FROM users
INNER JOIN user_counter ON users.id = user_counter.publisher_id
WHERE no =08123456789
AND counter < publisher_amount
)

我收到此错误:

操作数应包含 1 列

然后,我尝试使用 1 列,但结果不是我想要的。

我的问题是如何加入第一个和第二个查询?并且不会产生错误。

我已经尝试用谷歌搜索它,经过多次“试错”,这是我可以使查询工作的最远距离。

【问题讨论】:

  • 子查询在EXISTS (之后返回多于1列;)

标签: mysql mysql-error-1241


【解决方案1】:

我认为您只是在第二个子查询中错过了 EXISTS。无论如何,如果我正确理解您的查询,我认为您可以这样编写查询:

SELECT
  u.id
FROM
  users u inner join user_counter uc
  on u.id=uc.publisher_id
     and no=08123456789
     and counter < publisher_amount
WHERE
  u.publisher_set = '1'
  AND u.publisher_status = '1'
  AND u.publisher_content != ''
  AND u.publisher_amount != '0'
  AND u.publisher_now < publisher_max

【讨论】:

    【解决方案2】:

    您可以将第一个 EXISTS 子句更改为:

    and users.id in (select user_counter.publisher_id from user_counter) 
    

    并在最终查询中使用 EXISTS。

    【讨论】:

      【解决方案3】:

      你也可以这样做:

      AND EXISTS (
      
      SELECT user_counter.publisher_id
      FROM user_counter
      WHERE users.id = user_counter.publisher_id
      

      PS:由于某种原因,SQLFIDDLE 对我不起作用。否则会很乐意给你演示;)

      【讨论】:

      • 今天早上 SQL Fiddle 出现了一些奇怪的问题。现在应该可以工作了,谢谢。
      【解决方案4】:
      SELECT id
      FROM users
      WHERE publisher_set = '1'
      AND publisher_status = '1' 
      AND publisher_content != ''
      AND publisher_amount != '0'
      AND publisher_now < publisher_max
      AND EXISTS (
      select 1 from  user_counter where  users.id = user_counter.publisher_id
          and  no =08123456789
      AND counter < publisher_amount
      )
      

      假设nocounter 在桌子上user_counter。没有模式有点难以分辨。

      【讨论】:

        猜你喜欢
        • 2013-03-27
        • 1970-01-01
        • 2012-03-31
        • 2016-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-12
        相关资源
        最近更新 更多