I had 3 tables
1.usertb
email funcarea
2.professiontb
email experience
3.generalinfo
email location
I want to combine these three tables using the field email with 3 conditions like funarea = 2 and experience >=3 and location = 'Sydney'
Since I'm a beginner to SQL, I am eager to know how to implement this.
select a.email,funarea,experience,location from usertb a
left join professiontb b
on a.email = b.email
left join generalinfo c
on a.email = c.email
where
funarea = 2 and experience >=3 and location = 'Sydney'
If you have at least one entry in each table:
(blunt and stupid but it works)
SELECT *
FROM usertb, professiontb, generalinfo
WHERE usertb.email=professiontb.email AND professiontb.email=generalinfo.email AND usertb.funcarea=2 AND professiontb.experience>=3 AND generalinfo.location='Sydney';
If you don't have one entry in each table, my advice would be to use the LEFT JOIN statement.
SELECT * FROM usertb WHERE usertb.funcarea=2
LEFT JOIN professiontb ON(usertb.email=professiontb.email AND professiontb.experience>=3)
LEFT JOIN generalinfo ON(usertb.email=generalinfo.email AND generalinfo.location='Sydney');
As is, you should get all the rows from usertb, even if there is some missing ones in the other tables.
Related
Hello I am trying to sort through these 3 Tables
I need to create a query that goes through the 'Author' Table,
grabs the author num
Then goes to the 'Wrote' table to find the 'BookCode' from the AuthorNum of the last table
Then to finally go through the Book table to list the title of the book and the first name and last name of the author.
I was thinking of using a join table but am not too solid on my uinderstanding on how it works. Nested select statements was my next guess but I can't get them to go through so many tables.
If anyone could help me that would be fantastic, thank you.
You want to use INNER JOINS to match up the data
SELECT *
FROM authors AS a
INNER JOIN wrote AS w
ON a.AuthorNum = w.AuthorNum
INNER JOIN book AS b
ON w.BookCode = b.BookCode
Please try to use this :
(I named the first table name to first)
Select a.Title as title, w.AuthorFirst as firstName, w.AuthorLast as lastName
From wrote as w
Inner Join author as a
Inner Join first as f
On (Select ww.AuthorNum From WroteTable as ww Order By DESC LIMIT 1) = f.AuthorNum
On f.BookCode = a.BookCode
I have 2 tables, student and grades
student table contains id, name and date_of_birth
grades table contains id, student_id, grade and course
Actual table contain more data.
I have a query like
SELECT s.*, AVG(g.grade) as average_grade
FROM student s LEFT JOIN grade g ON s,id = g.student_id
WHERE g.course = 'mathematics' and s.id = 1
With this I could get the data i needed which are student details and the average grade, then come the problem where when the course = "mathematics" is not found in the grades table, the query will return NULL. My question is, is there a way for me to get the s.id = 1 details together with NULL average instead of all NULL value?
I would prefer if it is able to do it with 1 query, as because in my current I am using subquery and it takes very long to get the data. My main objective is to get more faster speed if you have better way instead of using 1 query feel free to comment your idea. In addition I have tried multiple query and sub query to get all the data but it all take too long.
Move your filter criteria for g.course = 'mathematics' in joining part
SELECT s.*, AVG(g.grade) as average_grade
FROM student s
LEFT JOIN grade g ON s.id = g.student_id AND g.course = 'mathematics'
WHERE s.id = 1
Your query produces result as inner join not left because putting g.course = 'mathematics in where clause turns your left join to inner join, Moving this part in on clause will still return data from student table if there were no rows found from grade table with course = 'mathematics'
If the course is not 'mathematics' you would still get the student data if you put it like this.
SELECT s.*, AVG(g.grade) as average_grade
FROM student s LEFT JOIN grade g ON s,id = g.student_id
WHERE (g.course = 'mathematics' AND s.id = 1) OR s.id = 1
SELECT COU.recipe_id, IFNULL(FOU.Found, 0) AS Found, COU.Count
FROM ( SELECT recipe_id, COUNT(recipe_id) As Count
FROM recipe_ingredients
GROUP BY recipe_id) COU
LEFT OUTER JOIN ( SELECT R.recipe_id, COUNT(R.key_ingredient) AS Found
FROM users_ingredients U
JOIN recipe_ingredients R ON R.key_ingredient = U.key_ingredient
GROUP BY R.recipe_id) FOU ON FOU.recipe_id = COU.recipe_id
I need to Join three tables together - well, 4 in the end. Can someone please show me how I can do this and explain how to keep joining when required later. I will have 4 or more tables. Many Thanks
Credit to #Arulkumar for Above
I don't really understand what your SQL does but, to join another table you need to do something like this.
SELECT recipe.id, all_recipes.id
FROM recipe
LEFT JOIN all_recipes ON recipe.id = all_recipes.id
I'm really struggling to get my head around this. I am trying to run a SELECT query from multiple tables.
This is what I have so far that doesn't work;
SELECT jira_issues.*, session_set.* FROM jira_issues, session_set
INNER JOIN reports on jira_issues.report_id = reports.id
WHERE jira_issues.report_id = 648
I have other tables (session_set, report_device) which has a ReportID and report_id column respectively.
I have a report table which has a Primary Key id. In the other tables the report.id key is linked with foreign keys.
Ultimately what I am trying to achieve is this:
I have an entry in the reports table with an id of 648. In the other tables (jira_issues, report_device, session_set), I also have entries which has a foreign key linked to the report id in the report table.
I want to run one SELECT Query to query the tables (jira_issues, report_device and session_set) and get all the data from them based on the report.id.
Thanks!
What about this:
SELECT * FROM jira_issues ji
LEFT JOIN session_set ss ON ji.report_id = ss.ReportID
LEFT JOIN report_device rd ON rd.report_id = ji.report_id
WHERE ji.report_id = 648;
Just say "no" to commas in the from clause. Always use explicit join syntax:
SELECT ji.*, session_set.*
FROM jira_issues ji inner join
reports r
on ji.report_id = r.id inner join
session_set ss
on ss.ReportId = r.report_id
WHERE ji.report_id = 648;
If some of the tables might have no corresponding rows, you might want left outer join instead of inner join.
Kindly try this out. You may get syntax error.
SELECT a., b. FROM jira_issues a, session_set b, reports c
Where a.report_id = c.id and b.report_id = c.id AND a.report_id = 648
I am encountering a problem I cannot bypass by myself and I that's why I am posting this question. There are a lot of other posts out there that give me half of the answer and I don't really know how to get it done.
I have 3 tables that contain informations about an ad. One table is "ad_names", another is "ad_locations" and the last one is "ad_details".
Ad_Names has : ad_title, ad_description, ad_date_added
Ad_Locations has : ad_country, ad_region, ad_city
Ad_Details has : ad_price, ad_author, ad_active
Basically I want apply location and details filters for an ad with a certain title.
For example, I want to search "food" keyword in ad-titles and then apply filters like "only from Kansas" or "Only from Kansas + price higher than 500USD". How do I do it?
first you will need to join the tables. do you know if you have a foreign key on the tables? like is there an Ad_ID field on all 3 tables? if the tables are large you may also want to index the fields you are searching. once you have all the data you will "filter" it with a where clause.
http://www.w3schools.com/sql/sql_where.asp
select * from ad_names as A
join ad_location as L on A.key=L.key
join ad_details as D on A.key=D.key
where A.ad_title like '%food%' and L.ad_region = 'Kansas' and D.ad_prics > 500;
depending on how your database is setup, you may get a cartesian result there, so you would have to look at your join. maybe a left outer join limiting the join in an on statement instead of in the where. it is hard to say without more information.
Join types
Assuming that Ad is a table with primary key id, which is a foreign key in each of the other tables (ad_id) then something along the lines of:
Location:
SELECT *
FROM Ad A
INNER JOIN Ad_Names N ON N.ad_id = A.id
INNER JOIN Ad_Locations L ON L.ad_id = A.id
INNER JOIN Ad_Details D ON D.ad_id = A.id
WHERE N.ad_title LIKE '%food%' AND L.ad_city = 'Kansas'
Price and location:
SELECT *
FROM Ad A
INNER JOIN Ad_Names N ON N.ad_id = A.id
INNER JOIN Ad_Locations L ON L.ad_id = A.id
INNER JOIN Ad_Details D ON D.ad_id = A.id
WHERE N.ad_title LIKE '%food%' AND L.ad_city = 'Kansas' AND D.ad_price > 500