2012-12-18 63 views
0

我有這樣的MySQL查詢(工作)MySQL錯誤:操作數應包含1列(S)

首先查詢:

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 
) 

我得到這個錯誤:

Operand should contain 1 column(s)

然後,我嘗試使用1列,但結果不是我想要的。

我的問題是如何加入第一個和第二個查詢?並沒有產生任何錯誤。

我已經嘗試過谷歌它和許多「嘗試錯誤」這是我可以讓查詢工作。

+0

子查詢返回多於1列;後'EXISTS)(' – bonCodigo

回答

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 
0

你可以改變第一EXISTS子句:

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

和使用存在於最終的查詢。

0

你也可以這樣做:

AND EXISTS (

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

PS:SQLFIDDLE不會在我最終出於某種原因。否則會很高興給你演示;)

+1

今天上午同SQL小提琴一些奇怪的問題。現在應該工作了,謝謝。 –

+0

@JakeFeasel噢,好主謝謝你:)。 – bonCodigo

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。如果沒有模式,有點難以分辨。

相關問題