我有这个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列,但结果不是我想要的。
我的问题是如何加入第一个和第二个查询?并且不会产生错误。
我已经尝试了谷歌它并且在经过多次“尝试和错误”之后,这是我可以使查询工作的最远。
答案 0 :(得分: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
答案 1 :(得分:0)
您可以将第一个EXISTS子句更改为:
and users.id in (select user_counter.publisher_id from user_counter)
并在最终查询中使用EXISTS。
答案 2 :(得分:0)
你也可以这样做:
AND EXISTS (
SELECT user_counter.publisher_id
FROM user_counter
WHERE users.id = user_counter.publisher_id
PS:由于某种原因,SQLFIDDLE在我的最终不起作用。其他人很乐意给你演示;)
答案 3 :(得分: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
)
假设no
和counter
在表user_counter
上。没有架构有点难以分辨。