Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a 2 table namely "tblSchedules" and "tblCity",In "tblSchedules" i have two columns with name 'source' and 'Destination'.
Both have a relation tblcity table to same column cityID ,how to write query using inner join .please guide me
Posted
Updated 8-May-15 20:36pm

Try:
SQL
SELECT s.*, 
       c.* 
FROM tblSchedules s
INNER JOIN tbtCity c
ON s.cityID = c.cityID


[edit]
"Markdown" stole the asterisks!
If you still can't see them, between the SELECT and the FROM should be an "s." and a "c." both followed by a "*"
[/edit]


[edit]
It helps if you are specific! :laugh:
You need a JOIN for each column in the schedules you want to replace with the city name:
SQL
SELECT s.ScheduleId, s.FlightId, c1.CityName AS [From], c2.CityName AS [To], s.Departure_Time, s.Arrival_Time, s.[Date]
FROM tblSchedules s
INNER JOIN tblCities  c1
ON c1.CityID = s.Source
INNER JOIN tblCities c2
ON c2.CityID = s.Destination

Will give you:
ScheduleId FlightId     From       To Departure_Time Arrival_Time      Date
         4        1   Mysore   Mumbai        5:30 PM     10:20 AM  5/9/2015

A couple of suggestions though: Dump the three columns "Departure_Time", "Arrival_Time", and "Date" and replace them with two DATETIME fields: "DepartAt" and "ArriveAt" - storing dates and times separately means using strings, and that causes all sorts of problems later on. Using DateTime values means you can do math, searching, and sorting on them a lot easier. (And it shows that the aircraft doesn't travel back in time, as it seems from your illustration! :laugh: )

[/edit]
 
Share this answer
 
v3
Comments
RAJU-11052090 9-May-15 3:06am    
thank you OriginalGriff
i tried the way ,but its not getting values from table.


SELECT tblCities.*, tblSchedules.*
FROM tblCities INNER JOIN
tblSchedules ON tblCities.CityID = tblSchedules.Source AND tblCities.CityID = tblSchedules.Destination
OriginalGriff 9-May-15 3:21am    
Show some of the sample data, together with exactly what you expect to get - it will help me visualise the connections better (SSMS has a "copy" facility in the row view)
RAJU-11052090 9-May-15 3:29am    
tblCities
CityId CityName
2 Mysore
3 Mumbai

tblSchedules
ScheduleId FlightId Source Destination Departure_Time Arrival_Time Date

4 1 2 3 5:30 PM 10:20 AM 5/9/2015
source and destination are connected to tblCities.CityId
RAJU-11052090 9-May-15 3:30am    
Sir

Source= 2 and Destination=3 coming from tblCities.CityId,
OriginalGriff 9-May-15 3:52am    
Answer updated.
PLEASE IGNORE THIS SOLUTION!
It's here to show Chris what I mean with a bug report:

There is no "disable markdown formatting" for solutions.
Within an SQL block:

SQL
SELECT s.*, c.*
FROM

There are no "*" characters
But on separate lines:
SQL
SELECT s.*, 
       c.*
FROM
They appear.
 
Share this answer
 
v3
In addition to solution 1 by OriginalGriff[^], i'd suggest to read this: Visual Representation of SQL Joins[^]. Note: there's at least few types of join.
 
Share this answer
 

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