0

我有这个 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 列,但结果不是我想要的。

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

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

4

4 回答 4

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
于 2012-12-18T18:02:46.747 回答
0

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

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

并在最终查询中使用 EXISTS。

于 2012-12-18T17:03:46.257 回答
0

你也可以这样做:

AND EXISTS (

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

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

于 2012-12-18T17:19:37.900 回答
0
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。没有模式有点难以分辨。

于 2012-12-18T17:47:29.050 回答