How To Write a Query That Is Based On Three Tables? - mysql

There are my tables. So basically I had to create a bunch of queries that were based off of two tables, which I did easily. The last question is asking to create a table that is based off of three tables, and the problem is I honestly cannot think of a way to reference all of these tables, it seems that there are not enough attributes in each table to reference three different ones.
Does anyone have any idea?
CRIME
crime_code PK
criminal_code FK
crime_desc
CRIMINAL
crim_code PK
jail_code FK
life_behind_bars
release_date
JAIL
jail_code PK
num_criminals_in_jail
cop_code FK
COP
cop_code
cop_Lname
cop_Fname
cop_phone

SELECT *
FROM CRIME c
INNER JOIN CRIMINAL cr ON c.criminal_code = cr.crim_code
INNER JOIN JAIL j ON cr.jail_code = j.jail_code
INNER JOIN COP cp ON j.cop_code = cp.cop_code
This is a simple select statement with multiple join. The first table CRIME joins to the second table CRIMINAL using the FK of CRIME to join to the PK of CRIMINAL, so on and so on.
Info on joins. http://www.w3schools.com/sql/sql_join.asp
This is all very basic stuff and can be found with a google search.

Related

count students from table where join mysql

I have the databaase in icon below. I want
to count all students from Subject with name Psychology and class with name Class5.
the percentage of students with status "Something" from subject with name Psychology and class with name Class5.
All students and the class name from Class "Class6" that are male.
I've tried for example
(in english:)
SELECT COUNT(student_name) AS NumberOfStudents FROM student_srms JOIN class_srms JOIN subject_srms WHERE class_srms.class_name='Class5' AND subject_srms.subject_name='Psychology'
But returns NumberOfStudents = 20, but 20 are all student entries.
The issue likely stems from your FROM clause. It's not enough to just say JOIN. You need to specify the relationship of the columns between the two tables being joined with an ON clause:
FROM student_srms
JOIN class_srms
ON student_srms.student_id = class_srms.student_id
JOIN subject_srms
ON class_srms.subject_id = subject_srms.subject_id
I believe in MySQL there is a NATURAL JOIN which will tell mysql without an ON clause to just join on column names that are similar between the two tables, but that feels dirty to me and could cause failures later on in an applications lifecycle if new columns are introduced to tables that share names, but not relationships, so I would just steer clear of that.
I have a suspicion that your diagram showing tables/columns is incorrect based on the error you are reporting in the comments. Instead, try (and I'm totally guessing blind here at this point):
FROM student_srms
JOIN student_class
On student_srms.student_id = student_class.class_id
JOIN class_srms
ON student_class.class_id = class_srms.student_id
JOIN subject_srms
ON class_srms.subject_id = subject_srms.subject_id
That adds in that student_class relationship table so you can make the jump from student to class tables. Fingers crossed.

Selecting/Inserting data between 3 tables

I think there are something wrong with my structure. I have 3 tables in the database. I want to show all the supply in a specific division. I did some trial and errors but I can't insert another supply in the same plan id. I wanted to add more supplies in one of specific division / specific plan id.Here's my query to select the supplies.
SELECT
division.acronym,
supply.`name`,
supply.unit,
supply.supply_id,
supply.price,
supply.estimated_budget,
supply.quantity
FROM
division
INNER JOIN plan ON plan.plan_id = division.division_id
INNER JOIN supply ON supply.supply_id = plan.plan_id
The table is only available at documentation. So, I insert my ERD diagram as an image.
ERD edited
I think you should put division_id and supply_id to plan table as FOREIGN KEYS to join all three tables.
Then your query should look like:
division
INNER JOIN plan ON plan.division_id=division.division_id
INNER JOIN supply ON supply.supply_id=plan.supply_id

Multi/level query in MySQl - is an extra foreign key necessary?

Lets say that I have 3 tables:
departments, each of which has 0..n
jobs, each of which has 0..n
people
Given a department, how do I get all the people who work in that department? Can I do it with a single SELECT?
I have set up a fiddle with some sample data, but I can't formulate the correct query.
Can it be done with some JOIN magic? Or do I need to add a foreign key in the peeps table, pointing back to the department_id?
A simple join
SELECT people.*
FROM departments
INNER JOIN jobs ON departments.deperatment_id = jobs.deperatment_id
INNER JOIN people ON jobs.job_id = people.job_id
WHERE departments.deperatment_id = 1
You will need to amend the column names in the join conditions to the ones used in your tables.
Note that your sample tables on SQL fiddle do not have any indexes. Adding these is VERY important for performance.

Query to Pull Data from 2 Junction Tables

I'm creating a database for comics. Right now I have 3 main tables (comics, publishers, people) and 3 junction tables (person2comic, publisher2comic and person2publisher). I want to be able to have a search form that allows searching by any combination of title, issue number, publisher, and person. When referencing only one junction table, I use a variation of this depending on what's being searched for:
SELECT comicTitle, comicIssue, firstName, lastName
FROM person2comic
JOIN comics ON comics.comicID = person2comic.comicID
AND comics.comictitle LIKE "%walk%" ;
If someone were to search by title, publisher and person, I'm not sure how to set up the statement since it would require using two of the junction tables. Would it be a nested query situation or something else?
You can have arbitrarily many joins. Not exactly sure on all of your column names, but this should roughly work:
SELECT *
FROM people
JOIN person2comic p2c ON people.id = ptc.person
JOIN comic ON p2c.comic = comic.id
JOIN publisher2comic pub2c ON comic.id = pub2c.comic
JOIN publisher ON pub2c.publisher = publisher.id
Also note that your schema may be inefficient if you relationships all aren't many-to-many. See my comment.

SQL - Joining tables BUT not always

I need to perform a query SELECT that joins three tables (no problem with that). Nonetheless, the third table can, or NOT, have any element that match the joining KEY.
I want ALL data from the first two tables and if the ITEMS have ALSO information in the third table, fetch this data to.
For example, imagine that the first table have a person, the second table have his/her address (everyone lives anywhere), the third table stores the driving license (not everyone has this) - but I need to fetch all data whether or not people (all people) have driving license.
Thanks a lot for reading, if possible to give you suggestion / solution!
Use LEFT JOIN to join the third table. Using INNER JOIN a row has to exists. Using LEFT JOIN, the 'gaps' will be filled with NULLs.
SELECT
p.PersonID, -- NOT NULL
-- dl.PersonID, -- Can be null. Don't use this one.
p.FirstName,
p.LastName,
a.City,
a.Street,
dl.ValidUntilDate
FROM
Person p
INNER JOIN Addresse a ON a.AddressID = p.HomeAddressID
LEFT JOIN DrivingLicence dl ON dl.PersonId = p.PersonID