views:

30

answers:

5

Hello,

I have these 2 mysql queryes

SELECT `name` AS name1,`id` AS id1 FROM `table` WHERE id=1
SELECT `name` AS name2,`id` AS name2 FROM `table` WHERE id=2

The results will be outputed like this

NAME1  ID1
john  1
NAME2  ID2
meg   2

Is there anyway that from 2 queries I would make 1 and it will show all results on 1 line from same table ?

NAME1   ID1  NAME2   ID2
john     1   meg     2
A: 

Do you mean something like using OR in the WHERE clause? Like:

SELECT `name`,`id` FROM `table` WHERE id=1 OR id = 2
Aaron Hathaway
This would produce two rows tho.
DrDipshit
A: 

You could combine the query like this

SELECT `name`,`id` FROM `table` WHERE id=1 OR id=2

but, you'll still get two results. What are you trying to do with the result output?

Ryan Twilley
+1  A: 
Select Min( Case When Id = 1 Then Id End ) As Id1
    , Min( Case When Id = 1 Then Name End ) As Name1
    , Min( Case When Id = 2 Then Id End ) As Id2
    , Min( Case When Id = 2 Then Name End ) As Name2
From `table`
Where Id In(1,2)
Thomas
+1: I prefer to use MAX - more obvious IMO
OMG Ponies
A curious down-vote. I'd be fascinated to know the reasoning.
Thomas
A: 

select tbl1.name,tbl1.id,tbl2.name,tbl2.id from mytable tbl1,mytable tbl2

but this query would be a mess, it will give you the whole combinations of two names in any order from your table, you could filter it with a WHERE clause but.. how many names do you have?? why doing that? maybe if we know what you're trying to do we might give you a better answer

EDIT ohhh i see your queries now... it should be something like this

select tbl1.name,tbl1.id,tbl2.name,tbl2.id from mytable tbl1,mytable tbl2  where tbl1.id=1 and tbl2.id=2
pleasedontbelong
+2  A: 
SELECT t1.name AS name1, t1.id AS id1, t2.name as name2, t2.id as id2
FROM table as t1, table as t2 WHERE t1.id = 1 and t2.id = 2;
carl