Click here to Skip to main content
15,878,430 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have two table FoodList and AlternateFoodList . I want All Food and Alternate Food For a person in an appointment

Appointment



AIDDate
1 1-1-2013
2 2-1-2014

FoodLsit

FoodAptID AIDFoodIDQTY
1 1 1 1
2 1 2 2


AlternateFoodList



FoodAlternateIDFoodAptID FoodIDQTY
11 3 1
21 5 2


requiredResult is


FoodAlternateIDFoodAptID AIDFoodIDQTYA_F
1 1 1 1 F
11 1 3 1 A
21 1 5 1 A


i tried query like this
SQL
 select null as 'FoodAlternateID',P. *,"F" as A_F from  FoodAptID P inner join Appointment A on A.AID=P.AID where A.Date ='1-1-2013'
union
(some query where i able to use A.Date ='1-1-2013' in sub table )

PLZ help
Posted
Updated 7-Jan-14 19:50pm
v5
Comments
Maciej Los 8-Jan-14 1:55am    
Please, be more specific and provide more details.
rajin kp 8-Jan-14 2:03am    
Need All Data from Tables with appointment date 1-1-2013 see result table .problem is that i didnt add AID in subtable insted it contain FoodAptID and Table FoodLsit has AID . Is this possible to use result we get before Union to use inside query after union .ie
(select null as 'FoodAlternateID',P. *,"F" as A_F from FoodAptID P inner join Appointment A on A.AID=P.AID where A.Date ='1-1-2013') as TT
union
(some query where i can USe TT to join with AlternateFoodList so that only reuired row come in here )
Maciej Los 8-Jan-14 2:07am    
You don't need to add AID into several tables. You need to know the relationships between tables. And based on these relationships, you will be able to get proper data.
Maciej Los 8-Jan-14 2:09am    
Please, provide more details about FoodAptID table.
rajin kp 8-Jan-14 3:51am    
FoodAptID is PK for FoodLsit and FK for AlternateFoodList

1 solution

Sorry, but your question is not clear still.

Have a look at example:
SQL
DECLARE @Appointment TABLE (AID INT IDENTITY(1,1),  [Date] DATETIME)
INSERT INTO @Appointment ([Date])
VALUES ('2013-01-01'), ('2014-01-02')

DECLARE @FoodLsit TABLE (FoodAptID INT IDENTITY(1,1), AID INT, FoodID INT, QTY INT)
INSERT INTO @FoodLsit (AID, FoodID, QTY)
VALUES(1, 1, 1), (1, 2, 2)

DECLARE @AlternateFoodList TABLE (FoodAlternateID   INT IDENTITY(1,1), FoodAptID INT, FoodID INT, QTY INT)
INSERT INTO @AlternateFoodList (FoodAptID, FoodID, QTY)
VALUES(1, 3, 1), (1, 5, 2)


SELECT AFL.FoodAlternateID , FL.FoodAptID, AFL.FoodID, FL.QTY , A.AID, A.Date
FROM @AlternateFoodList AS AFL INNER JOIN @FoodLsit AS FL ON AFL.FoodAptID = FL.FoodAptID INNER JOIN @Appointment AS A ON FL.AID = A.AID
WHERE A.Date = '2013-01-01'


For further information about JOIN's, please see: Visual Representation of SQL Joins[^]
 
Share this answer
 
Comments
rajin kp 9-Jan-14 2:20am    
select null as 'FoodAlternateID',P. *,"F" as A_F from FoodAptID P inner join Appointment A on A.AID=P.AID where A.Date ='1-1-2013'
union
select S.* from AlternateFoodList S inner join ( select null as 'FoodAlternateID',P. *,"F" as A_F from FoodAptID P inner join Appointment A on A.AID=P.AID where A.Date ='1-1-2013')PP on PP.FoodAptID =S.FoodAptID see the second part of union it use sane result in first part ie PP is same as in first part can i use result in first part At PP,ie my question.Actually there are more table in join .i fear that using join @ both side of union will reduce speed :(
rajin kp 9-Jan-14 2:22am    
i m not looking for how join work :D

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