提问人:mohamed elghamry 提问时间:9/29/2023 最后编辑:Barmarmohamed elghamry 更新时间:9/29/2023 访问量:20
使用 select 将 where 与主列一起使用 mysql join
mysql join by using select have where with master column
问:
select t.* from
(
SELECT p.*
,b.id as brand
,b.name as brandname
,j.id as closed
,c.name as customername
,l.name as productline
,u.user_name as username
,TIMESTAMPDIFF(HOUR,j1.start_date,j2.delivery_date) as total_hour
FROM `project` AS p
left join customer as c on c.id = p.customer
left join brand as b on b.id = c.brand
left join customer_product_line as l on l.id = p.product_line
left join users as u on u.id = p.created_by
left join (select project_id,`status`,id from job where `status` <> '1' group by project_id,`status`) as j on j.project_id = p.id
left join (select project_id,start_date from job where project_id = id order by start_date asc limit 1) as j1 on j1.project_id = p.id
left join (select project_id,delivery_date from job where project_id = id order by delivery_date DESC limit 1) as j2 on j2.project_id = p.id
) t
我在最后 2 个左连接中出错p.id
通过函数或程序或其他方式执行此操作的任何方法
答:
0赞
Barmar
9/29/2023
#1
不能在 .JOIN
与其在子查询中使用,不如在其列表中使用。您可以使用单个查询来获取 和 。ORDER BY <column> DESC LIMIT 1
MAX(<column>)
SELECT
MAX(start_date)
MAX(delivery_date)
SELECT p.*
,b.id as brand
,b.name as brandname
,j.id as closed
,c.name as customername
,l.name as productline
,u.user_name as username
,TIMESTAMPDIFF(HOUR,j1.max_start,j1.max_delivery) as total_hour
FROM `project` AS p
left join customer as c on c.id = p.customer
left join brand as b on b.id = c.brand
left join customer_product_line as l on l.id = p.product_line
left join users as u on u.id = p.created_by
left join (select project_id,`status`,id from job where `status` <> '1' group by project_id,`status`) as j on j.project_id = p.id
left join (
select project_id,MAX(start_date) AS max_start, MAX(delivery_date) AS max_delivery
from job
GROUP BY project_id
) as j1 on j1.project_id = p.id
评论
0赞
mohamed elghamry
9/29/2023
非常感谢............
评论
where project_id = id
ON j1.project_id = p.id