Click here to Skip to main content
14,978,778 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everyone,

I have two tables t1 and t2
table t1--

id     name
1       a
2       b
3       c
4       d
5       e

table t2 --

id     name
2       c
3       r
5       z

i want output using a single line query as :

id    name
1      a
2      c
3      r
4      d
5      z







that is when the id column matches in table t1 and t2 then it should select name from table t2 else from table t1

i have done this using temporary tables..but i want it in a single line query

please help me out..

thanks & regards
krunal panchal
Posted

1 solution

this way...
SQL
select t1.id,case when t2.name is null then t1.name else t2.name end as name
from t1
left join t2 on t1.id=t2.id

Happy Coding!
:)
   
Comments
Ankur\m/ 7-Feb-13 3:20am
   
Good that I refreshed before posting. This is exactly the same query I have written. And I think it should do the job. 5!
Aarti Meswania 7-Feb-13 3:21am
   
thank you! :)
krunalpanchalN 7-Feb-13 3:22am
   
thanks a lot :)
Aarti Meswania 7-Feb-13 3:24am
   
welcome! :)
Glad to help you! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900