SitePoint Sponsor |
|
User Tag List
Results 1 to 3 of 3
Thread: query from 3 tables in 1
-
Dec 11, 2009, 15:31 #1
- Join Date
- Apr 2004
- Location
- Boston
- Posts
- 482
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
query from 3 tables in 1
Hello,
I am trying to query specific information from 3 tables, the only relationship in the data is that it was selected from the same form.
here is my table info
Table: cities
columns: cityID, cityName, cityState
Table: users
columns: userID, userName, userEmail
Table: jobs
columns: jobID, jobName, jobStart
Individually the queries would be:
select cityName from cities where cityID = '1'
select userName, userEmail from users where userID = '53'
select jobName from jobs where jobID='2'
Is there a way to combine these into one query?
I will be displaying the results back using PHP.
Thank You.
-
Dec 11, 2009, 18:03 #2
- Join Date
- May 2005
- Location
- Cardiff
- Posts
- 1,832
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Afaik (and I'm no expert) but those are then three different queries, you can't do a join if there is no relationship between the tables and you can't select data from three different tables without a join
But it appears you could very well establish a relationship between them, for example the job table could get a field cityID to identify the city the job is located.
More info on joins: http://www.w3schools.com/sql/sql_join.aspDan G
Marketing Strategist & Consultant
-
Dec 11, 2009, 21:48 #3
- Join Date
- Jul 2002
- Location
- Toronto, Canada
- Posts
- 39,347
- Mentioned
- 63 Post(s)
- Tagged
- 3 Thread(s)
yup
Code:SELECT 'city' AS resulttype , cityName AS result1 , NULL AS result2 FROM cities WHERE cityID = 1 UNION ALL SELECT 'user' , userName , userEmail FROM users WHERE userID = 53 UNION ALL SELECT 'job' , jobName , NULL FROM jobs WHERE jobID = 2
Bookmarks