0

I have a subquery that returns 3 columns. I cannot get rid of them. I want to only use one of the columns for the parent query.

For example.

SELECT * FROM table_a WHERE id = (SELECT a,id,b FROM table_b LIMIT 1);

I want to only get id from table_b.

I'm looking for something along the lines of:

SELECT * FROM table_a WHERE id = (SELECT a,id,b FROM table_b LIMIT 1)[id];

How would I accomplish this?

2
  • SELECT * FROM table_a WHERE id = (SELECT id FROM table_b LIMIT 1); you cannot use like this? Commented Oct 17, 2012 at 6:08
  • the query is much more complex, the additional columns i cannot get rid of Commented Oct 17, 2012 at 6:22

4 Answers 4

3

how abt this:

SELECT * FROM table_a WHERE id =
(select id from
 (SELECT a,id,b FROM table_b LIMIT 1)a);
Sign up to request clarification or add additional context in comments.

2 Comments

Lol. Yep. Didn't realize it was that easy
This was a good solution! I was selecting a value with MAX but needed the ID (and so had to select two columns) in a subquery. This solved the problem.
0
SELECT * 
FROM table_a 
WHERE id IN
    (
        SELECT a FROM tableB limit 1
        UNION
        SELECT id FROM tableB limit 1
        UNION
        SELECT b FROM tableB limit 1
    ) 

Comments

0
SELECT * FROM table_a, (SELECT a,id,b FROM table_b LIMIT 1) temp_b where table_a.id = temp_b.id;

Is this result match your mind?

Comments

0

I think the best way of achieving this is :

SELECT * 
FROM table_a
inner join 
 (
       SELECT a as id FROM tableB
        UNION
        SELECT id FROM tableB
        UNION
        SELECT b as id FROM tableB
) tempB on (table_a.id=tempB.id)

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.