3

Instead of running hundreds of SELECT queries for one operation, I want to just run one big query, which I'm hoping will ease the load on my server.

SELECT (
(SELECT link_type_id FROM connections WHERE (node_to_id = '0' AND node_from_id = '1') OR (node_from_id = '1' AND node_to_id = '0')),
(SELECT link_type_id FROM connections WHERE (node_to_id = '0' AND node_from_id = '2') OR (node_from_id = '2' AND node_to_id = '0'))
)

There will be many more SELECTS in this query, but even the two aren't working. When I run this code I get the error:

Operand should contain 1 column(s).

Any suggestions? Thanks!

4
  • You need to use UNION SELECT and lose the outer SELECT, it won't really be that much more efficient though. Commented Apr 9, 2013 at 4:06
  • what is your criteria for selection? (node_to_id = '0' AND node_from_id = '1') OR (node_from_id = '1' AND node_to_id = '0') doesnt seem to differ.. both options are the same Commented Apr 9, 2013 at 4:07
  • @doublesharp it's less about efficiency, more that I want to reduce the number of time I query the database. Commented Apr 9, 2013 at 7:09
  • That's what I meant, a UNION is a separate query with it's own execution plan even as part of a larger query. Commented Apr 9, 2013 at 15:57

2 Answers 2

7

You can try below may be but you may need UNION

SELECT link_type_id FROM connections 
WHERE (node_to_id = '0' AND node_from_id = '1') 
OR (node_from_id = '1' AND node_to_id = '0')
UNION
SELECT link_type_id FROM connections 
WHERE (node_to_id = '0' AND node_from_id = '2') 
OR (node_from_id = '2' AND node_to_id = '0')
Sign up to request clarification or add additional context in comments.

Comments

3

This is caused by the parentheses outside of the columns. Even something as simple as:

SELECT ((SELECT 1), (SELECT 1))

Will yield this error -- the problem is that MySQL can only display one column per ... well ... column, and the entire () wraps a single column. If you remove the outer parentheses it will display each SELECT in a separate column.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.