I am trying to join a 3 tables. projects, statuses, clients. The GOAL of the query should result in all projects that have a current status of "Received". I am also trying to grab the client name for displaying in the resulting table.
These tables have common foreign keys as: client_ID, status_ID.
Here is my query thus far. I am trying hard to get a better understanding of joins. If you can provide comments on what I am doing wrong and example code that would be very much appriciated.
SELECT clients.clientName,
clients.Client_ID,
projects.Client_ID,
projects.projectNumber,
projects.projectName,
projects.expectedDate,
statuses.Status_ID,
statuses.status
FROM projects,
clients,
statuses
LEFT JOIN clients on projects.Client_ID = clients.Client_ID
LEFT JOIN statuses on projects.Status_ID = statuses.Status_ID
WHERE status = 'Received'
Try using 'JOIN' instead of 'LEFT JOIN'. Also remove clients and statuses in the from clause. I would also query the Status_ID instead of the status name.
SELECT clients.clientName,
clients.Client_ID,
projects.Client_ID,
projects.projectNumber,
projects.projectName,
projects.expectedDate,
statuses.Status_ID,
statuses.status
FROM projects
JOIN clients on projects.Client_ID = clients.Client_ID
JOIN statuses on projects.Status_ID = statuses.Status_ID
WHERE statuses.status = 'Received'
just try with below code.
Remove LEFT JOIN and just use WHERE condition with INNER JOIN...
SELECT clients.clientName,
clients.Client_ID,
projects.Client_ID,
projects.projectNumber,
projects.projectName,
projects.expectedDate,
statuses.Status_ID,
statuses.status
FROM projects,
clients,
statuses
WHERE
clients on projects.Client_ID = clients.Client_ID AND
statuses on projects.Status_ID = statuses.Status_ID AND
status = 'Received'
This may be helpful to you and give records what you are suppose to waiting for.
You should only have one table-name in the FROM-clause, or else you'll be joining tables multiple times (ANSI-syntax). Also it helps to set the join-conditions the other way around (easier to read I think); and to expand the table names (projects.status=...) instead of (status=...).
Also, as already pointed out, "JOIN" instead of "LEFT JOIN" would force corresponding rows to exist if the 'projects' row are to be returned.
SELECT clients.clientName, clients.Client_ID, projects.Client_ID,
projects.projectNumber,projects.projectName, projects.expectedDate,
statuses.Status_ID, statuses.status
FROM projects
LEFT JOIN clients ON clients.Client_ID = projects.Client_ID
LEFT JOIN statuses ON statuses.Status_ID = projects.Status_ID
WHERE statuses.status = 'Received'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.