Mysql Custom select query
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
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;
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)
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
Do you mean something like using OR in the WHERE clause? Like:
SELECT `name`,`id` FROM `table` WHERE id=1 OR id = 2
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?
精彩评论