[英]MySql join issue 3 tables
I am trying to join a 3 tables. 我想加入3桌。 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. 这些表具有以下公共外键: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'. 尝试使用'JOIN'而不是'LEFT JOIN'。 Also remove clients and statuses in the from clause.
还要删除from子句中的客户端和状态。 I would also query the Status_ID instead of the status name.
我还会查询Status_ID而不是状态名称。
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... 删除LEFT JOIN并使用INNER JOIN的WHERE条件...
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). 您应该只在FROM子句中有一个表名,否则您将多次连接表(ANSI语法)。 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=...).
并扩展表名(projects.status = ...)而不是(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. 另外,正如已经指出的那样,如果要返回'projects'行,“JOIN”而不是“LEFT JOIN”会强制存在相应的行。
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'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.