操作数应该在mysql中有1列

时间:2013-11-13 20:10:45

标签: mysql sql subquery

我收到错误“操作数应该包含1列”。

是的,我尝试将1列与3进行比较,那么我想实现的目标是什么?

清楚例子:我有一个表测试

 id   profile_id  result_number
 10     1232        3
 10     3263        5
 10     2222        4
 10     2321        1 

实际上,我有3个查询,我想在一个查询中得到所有。首先,我得到profile_id 2222的结果编号(返回4)

 SELECT `result_number` FROM test WHERE id=10 AND `profile_id`=2222

接下来,我得到了具有result_number - 1

的profile_id
SELECT `profile_id` FROM test
 WHERE id=10 AND `result_number` = 4 - 1

(返回3)

最后,我得到了profile_id谁有result_number + 1

SELECT `profile_id` FROM test WHERE id=10 AND `result_number` = 4 + 1 

(返回5)

结果预期:

profile_id
    3263
    2222
    1232

有可能实现吗?

3 个答案:

答案 0 :(得分:3)

如果子查询包含多行,则可以JOIN

SELECT profile_id
FROM test t, (
  SELECT result_number
  FROM test
  WHERE id = 10
  AND profile_id = 2222
) q
WHERE t.result_number BETWEEN q.result_number-1 AND q.result_number+1
AND id = 10
ORDER BY t.result_number DESC

DEMO:http://sqlfiddle.com/#!2/6b922/1

答案 1 :(得分:0)

with a  as
(
SELECT `result_number`, profile_id FROM test WHERE id=10 AND `profile_id`=2222
)

SELECT PROFILE_ID FROM A
UNION
SELECT B.PROFILE_ID FROM TEST AS B, A WHERE B.RESULT_NUMBER = (A.RESULT_NUMBER - 1)
UNION
SELECT C.PROFILE_ID FROM TEST AS C, A WHERE C.RESULT_NUMBER = (A.RESULT_NUMBER + 1)

答案 2 :(得分:0)

我认为这会做你想要的:

select *
from test 
where id=10 and ( result_number= 4 or result_number=4-1 or result_number = 4+1)