How to SELECT data that connect in many to many relationship table - sql

I want to select all components in material table and the equipment_name in equipment table.
The equipment_name is connected with the id_material and id_equipment. 1 material can consist of many equipment name and so the equipment.
I tried to use this code in MS Access query but it said Syntax Error (missing operand).
SELECT material.id_material, material.part_number_material, material.material_description,material.brand, material.stock, material.um, equipment.equipment_name, material.type, material.location, material.remarks FROM equipment_list a INNER JOIN material b ON a.PKid_material = b.id_material INNER JOIN equipment c ON a.PKid_equipment = c.id_equipment;
Example Data
I am a beginner.

If you're using Access, then you need parentheses for a three way join:
SELECT
m.id_material,
m.part_number_material,
m.material_description,
m.brand,
m.stock,
m.um,
e.equipment_name,
m.type,
m.location,
m.remarks
FROM
(equipment_list el INNER JOIN material m
ON el.PKid_material = m.id_material)
INNER JOIN equipment e
ON el.PKid_equipment = e.id_equipment;

Related

Join tables when 3 column in first table that can point to same column in second table

I have the following DB structure:
And right now I can't make up a query to get
a creator data, admin data and tech data from item_contacts...
What kind of JOIN I need to use and how?
I think you want 3 joins on item_contacts - one for each column whose data you want to recover:
select
i.*,
cc.data as creator_data,
ca.data as admin_data,
ct.data as tech_data
from items i
inner join item_contacts cc on cc.contact_id = i.creator_id
inner join item_contacts ca on ca.contact_id = i.admin_id
inner join item_contacts ct on ct.contact_id = i.tech_id

Inner Join and Left Join on 5 tables in Access using SQL

I am attempting to access data from the following tables:
OrgPlanYear
ProjOrgPlnYrJunction
DC
DCMaxEEContribLevel
DCNonDiscretionaryContribLevel
Basically, I need to inner join OrgPlanYear + DC and ProjOrgPlnYrJunction then I need to Left Join the remaining tables (tables 4 and 5) due to the fact the tables 1-3 have all the rows I need and only some have data in tables 4-5. I need several variables from each table. I also need the WHERE function to be across all fields (meaning I want all this data for a select group where projectID=919).
Please help!
I have tried many things with errors including attempting to use the Design Query side (i.e. JOIN function issues, badly formatted FROM function, etc.)! Here is an example of one excluding all variables I need:
SELECT
ProjOrgPlnYrJunction.fkeyProjectID, OrgPlanYear.OrgName, DC.PlanCode, DCNonDiscretionaryContribLevel.Age,DCNonDiscretionaryContribLevel.Service
FROM
(((OrgPlanYear INNER JOIN DC ON OrgPlanYear.OrgPlanYearID = DC.fkeyOrgPlanYearID) INNER JOIN ProjOrgPlnYrJunction ON OrgPlanYear.OrgPlanYearID = ProjOrgPlnYrJunction.fkeyOrgPlanYearID)
LEFT JOIN
(SELECT DCNonDiscretionaryContribLevel.Age AS Age, DCNonDiscretionaryContribLevel.Service AS Service FROM DCNonDiscretionaryContribLevel WHERE ProjOrgPlnYrJunction.fkeyProjectID)=919)
LEFT JOIN (
SELECT DCMaxEEContribLevel.EEContribRoth FROM EEContribRoth WHERE ProjOrgPlnYrJunction.fkeyProjectID)=919)
ORDER BY OrgPlanYear.OrgName;
Main issues with your query:
Missing ON clauses for each LEFT JOIN.
Referencing other table columns in SELECT and WHERE of a different subquery (e.g., FROM DCNonDiscretionaryContribLevel WHERE ProjOrgPlnYrJunction.fkeyProjectID).
Unmatched parentheses around subqueries and joins per Access SQL requirements.
See below adjusted SQL that now uses short table aliases. Be sure to adjust SELECT and ON clauses with appropriate columns.
SELECT p.fkeyProjectID, o.OrgName, DC.PlanCode, dcn.Age, dcn.Service, e.EEContribRoth
FROM (((OrgPlanYear o
INNER JOIN DC
ON o.OrgPlanYearID = DC.fkeyOrgPlanYearID)
INNER JOIN ProjOrgPlnYrJunction p
ON o.OrgPlanYearID = p.fkeyOrgPlanYearID)
LEFT JOIN
(SELECT Age AS Age, Service AS Service
FROM DCNonDiscretionaryContribLevel
WHERE fkeyProjectID = 919) AS dcn
ON dcn.fkeyProjectID = p.fkeyOrgPlanYearID)
LEFT JOIN
(SELECT EEContribRoth
FROM EEContribRoth
WHERE fkeyProjectID = 919) AS e
ON e.fkeyProjectID = p.fkeyProjectID
ORDER BY o.OrgName;

How to fix query with multiple joined tables?

I have a main table M (Movies) and other tables L (Location), G (Genre), and S (Sub Genre). Each of the "other" tables are in a one to many relationship to table M, using.
I want to list all the Blu Ray titles and pull in their Location, Length (Time), Comments, Genre, and Sub Genre.
My query is:
SELECT L.Location, M.Title, M.Length, M.Comments, G.Genre, S.SubGenre
FROM ((L
INNER JOIN M ON M.Location = L.ID)
INNER JOIN G ON M.Genre = G.ID)
INNER JOIN SubGenre ON M.SubGenre = SubGenre.ID
ORDER BY M.ID
WHERE M.Type is "BluRay"
ORDER BY M.ID;
It gives me a subset of what the subset (26) of what the total number of records should be (447.)
1. Do I have the proper table relationships?
2. Do I really need the parentheses? (error without them)
3. How do I change my query to give me all the Location records, with the appropriate movie-related information?
4. What if I want to add additional tables?
The DB schema:
-- Note that Type and Length are in between square brackets, because those are reserved words.
-- Avoid use of reserved words with MovieType and MovieLength
SELECT
L.LocationName
, M.Title
, M.[Length]
, M.Comments
, G.GenreName
, S.SubGenreName
FROM Movies M
INNER JOIN Location L ON L.LocationID = M.LocationID
INNER JOIN Genre G ON G.GenreID = M.GenreID
INNER JOIN SubGenre S ON S.SubGenreID = M.SubGenreID
WHERE M.[Type] = 'BluRay'
ORDER BY M.MovieID
You need to JOIN on shared table columns.
For "How to change your query to give all Location records, with appropriate movie-related information" that depends on what you think is appropriate.
You should not need the parentheses. Unless you are using a SQL database I am not familiar with.
You do not need to put the INNER in because the default JOIN is INNER JOIN in all flavors of SQL databases. You also have 2 ORDER BY M.ID you only want the one after the WHERE.
I am not sure what you mean by more tables do you mean you tables to the JOIN or actually more tables?

3 tables to join

I am stuck in my liaison of my 3 tables.
I have a table which is candidates with 3 fields (id_candidate, name_candidate, firstname_candidate)
An other table nammed lessons with 5 fields (id_lesson, price_lesson, date_lesson, fk_candidate, fk_monitor)
I have a table which is monitors with 3 fields (id_monitor, name_monitor, firstname_monitor)
I can join 2 tables (candidates and lessons)
Here is the request
SELECT *
FROM lessons INNER JOIN
candidates
ON lessons.fk_candidate=candidates.id_candidate
ORDER BY id_candidate ASC
But my problem is that. I don't understand how to join 3 tables?
In fact, the name of monitor must appear on the table lessons.
I have tried that
SELECT *
FROM candidates id_candidate INNER JOIN
lessons id_lesson
ON lessons.fk_candidate = candidates.id_candidate INNER JOIN
monitors id_monitor
ON lessons.fk_monitor = monitors.id_monitor;
Presumably, something like this:
SELECT c.*, l.*, m.name_monitor -- list out the columns you want explicitly
FROM candidates c INNER JOIN
lessons l
ON l.fk_candidate = c.id_candidate INNER JOIN
monitors m
ON l.fk_monitor = m.id_monitor;
Notes:
Using table aliases is a good idea. I recommend abbreviations for the table name.
After you have defined the table alias, use them. Your on clauses still referred to the table name.
Explicitly list the columns that you want.

SQL statement with union join or left join

database table
I have this SQL problem to find the total count of the disable people between 2 table
The condition is I only want to get the ID with appearing in the recipient table
left join graph
I only want to get the total count of the left side data which link together with the a_children
http://sqlfiddle.com/#!9/2ca178/1
SELECT COUNT(*)
FROM recipient r
LEFT JOIN a_children c
ON r.hp_id = c.hp_id
AND c.health='OKU'
WHERE r.disability = 'YES'