Mysql 一个表中缺少ID,是否仍将其包含在结果中
在我的mySQL数据库中,我有两个名为job_details和job_actions的表。在job_details表中,如果我得到job_ID 41230的Detail_ID,那么我会得到五个结果。例如:Mysql 一个表中缺少ID,是否仍将其包含在结果中,mysql,sql,Mysql,Sql,在我的mySQL数据库中,我有两个名为job_details和job_actions的表。在job_details表中,如果我得到job_ID 41230的Detail_ID,那么我会得到五个结果。例如: select Detail_ID from job_details where Job_ID = '41230'; select details.Detail_ID from job_details details left join job_actions actions using (
select Detail_ID from job_details where Job_ID = '41230';
select details.Detail_ID from job_details details
left join job_actions actions using (Detail_ID)
where actions.Detail_ID IS NULL and details.Job_ID = '41230';
我想做的是使用相同的作业ID从作业操作表中获取每个细节ID的完成百分比。例如,这只生成4条记录,因为并非所有的详细信息ID都显示在此表中:
select Detail_ID, Percent_Complete from job_actions where Job_ID = '41230';
当我尝试连接两个表时,我得到相同的四条记录:
select
details.Detail_ID,
actions.Percent_Complete
from
job_details details,
job_actions actions
where
details.Job_ID = '41230' and
details.Job_ID = actions.Job_ID and
details.Detail_ID = actions.Detail_ID;
我希望我的输出包括在job_details表中找到的每个details_ID,即使在job_actions表中找不到它。例如:
select Detail_ID from job_details where Job_ID = '41230';
select details.Detail_ID from job_details details
left join job_actions actions using (Detail_ID)
where actions.Detail_ID IS NULL and details.Job_ID = '41230';
我知道如何查找job_actions表中缺少的Detail_ID,但不知道如何将其包含在结果中。例如:
select Detail_ID from job_details where Job_ID = '41230';
select details.Detail_ID from job_details details
left join job_actions actions using (Detail_ID)
where actions.Detail_ID IS NULL and details.Job_ID = '41230';
即使作业操作表中缺少详细信息_id87679,我如何在结果中包含该信息?切勿在from
子句中使用逗号。始终使用正确、明确、标准的JOIN
语法
您只需要一个左连接
:
select details.Detail_ID, actions.Percent_Complete
from job_details details left join
job_actions actions
on details.Job_ID = actions.Job_ID and
details.Detail_ID = actions.Detail_ID
where details.Job_ID = 41230; -- I assume Job_ID is a number so the single quotes are not necessary
切勿在FROM
子句中使用逗号。始终使用正确、明确、标准的JOIN
语法
您只需要一个左连接
:
select details.Detail_ID, actions.Percent_Complete
from job_details details left join
job_actions actions
on details.Job_ID = actions.Job_ID and
details.Detail_ID = actions.Detail_ID
where details.Job_ID = 41230; -- I assume Job_ID is a number so the single quotes are not necessary
此后,您编写了
连接的语法
,但使用了较旧的样式,其中where子句可能会变成实际的内部连接或相等连接
因此,使用适当的显式join
语法和left join
select jd.Detail_ID, ja.Percent_Complete
from job_details jd left join job_actions ja on
ja.Job_ID = jd.Job_ID and
ja.Detail_ID = jd.Detail_ID
where jd.Job_ID = '41230';
您也可以使用子查询
,因为您正在从作业详细信息
表中查找所有详细信息
select Detail_ID,
(select Percent_Complete from job_actions
where Job_ID = jd.Job_ID and
Detail_ID = jd.Detail_ID) as Percent_Complete -- Use limit with order by clause in case one or more Percent found.
from job_details jd
where Job_ID = '41230';
我怀疑,如果您将
Job\u ID
作为数字类型,那么您不需要使用引号,只需要使用value(即41230)因为您编写了连接的语法,但是使用了较旧的样式,带有where子句,可能会变成实际的内部连接或相等连接
因此,使用适当的显式join
语法和left join
select jd.Detail_ID, ja.Percent_Complete
from job_details jd left join job_actions ja on
ja.Job_ID = jd.Job_ID and
ja.Detail_ID = jd.Detail_ID
where jd.Job_ID = '41230';
您也可以使用子查询
,因为您正在从作业详细信息
表中查找所有详细信息
select Detail_ID,
(select Percent_Complete from job_actions
where Job_ID = jd.Job_ID and
Detail_ID = jd.Detail_ID) as Percent_Complete -- Use limit with order by clause in case one or more Percent found.
from job_details jd
where Job_ID = '41230';
我猜想,如果你有Job\u ID
作为数字类型,那么你不需要使用引号,只需要使用value(即41230)就可以了!然而,你说“千万不要在from子句中使用逗号。”为什么不呢?@ChrisNielsen。因为表达连接的正确方法是使用正确、明确、标准的连接语法。隐式笛卡尔积只是草率的逻辑。效果很好!然而,你说“千万不要在from子句中使用逗号。”为什么不呢?@ChrisNielsen。因为表达连接的正确方法是使用正确、明确、标准的连接语法。隐式笛卡尔积只是一种松散的逻辑。使用子查询是个好主意。谢谢使用子查询的好主意。谢谢