Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

Sql Joining Issue

$
0
0

Hello folks,

I have a requirement where I am showing price from food table based on food category if food category is matching it’s working fine , but what I want is if there is a match it will print the matching price and if no match it will print the GENERAL price

Please suggest inner join is not working and if I put left its showing null price

createtable emp(id intprimarykey, fnamevarchar(20),food_catvarchar(20))

createtable food(id intprimarykey, food_catvarchar(20),pricedecimal(10,2))

insertinto emp(id,fname,food_cat)values(1,'viky','Executive')

insertinto emp(id,fname,food_cat)values(2,'jay','managers')

insertinto food(id,food_cat,price)values(1,'GENERAL',100)

insert into food(id,food_cat,price) values(2,'Executive',250)

insert into food(id,food_cat,price) values(3,'CEO',500)

select e.*,f.pricefrom emp e left join food f on

e.food_cat= f.food_cat

--output

idfname food_catprice
1viky Executive250.00
2jaymanagers NULL

Desired Output

1viky Executive250.00
2jaymanagers 100

Thanks


--Thanks, Amit Kala Please remember to click “Mark as Answer” on the post that helps you, & to click “Unmark as Answer” if a marked post does not actually answer your question. it encourages us to help you.




Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>