2011-03-07 4 views
1

에 가입 I 기존 쿼리를 가지고있는 PHP 페이지에 정의되어MySQL은 계산 된 최대 결과

select ProductLinesID, ProductID, ProductName, ProductCatalog, ManufacturerName, 
     productMSDSStatus, productStatusDesc, productStatusIcon, DATE_FORMAT(dateAdded,'%d/%m/%Y') As dateAdded, DATE_FORMAT(ProductRevision,'%d/%m/%Y') as ProductRevision, 
     ManufacturerID, SupplierName, ProductID, DATE_FORMAT(dateLatestCheck, '%d/%m/%Y') as dateLatestCheck,s.SupplierID 
    from sds_productlines pl 
     right join sds_products p on p.ProductID = pl.productlinesProductID 
     left join sds_manufacturer m on p.ProductManufacturer = m.ManufacturerID 
     left join sds_product_status ps on p.productMSDSStatus = ps.productStatusID 
     left join sds_departments d on pl.ProductLinesDepartmentID = d.DepartmentID 
     left join sds_hospitals h on h.hospitalID = d.DepartmentHospitalID 
     left join sds_supplier s on s.SupplierID = pl.SupplierID 

. 다른 테이블에 저장된 다른 매개 변수를 추가하라는 요청을 받았습니다. 문제는 sds_product에 date_created라는 주석이 기본적으로 많은 "통신"을 가질 수 있다는 것입니다. 내가 특정 제품에 대한 통신의 목록을 원하는 경우 예를 들어, 내가 할 것 :

select * from sds_product_comms as pc 
join sds_comms c on pc.comms_id = c.comms_id 
where prod_id = 2546 

내가 SQL에서 직접이하고 싶었다, 그래서 어떻게 든이 두 가지를 고수하는 하위 쿼리를 만드는 것이 가능하다 , 초기 쿼리에서 중복 행을 만들지 않습니다.

예를 들어 동일한 최대 date_created를 가진 동일한 제품의 5 행을 원하지 않습니다.

desc sds_comms 

'comms_id', 'int(10) unsigned', 'NO', 'PRI', '', 'auto_increment' 
'method', 'int(10) unsigned', 'NO', '', '', '' 
'dialogue', 'varchar(200)', 'NO', '', '', '' 
'reply_id', 'int(10) unsigned', 'NO', '', '', '' 
'comm_to', 'varchar(60)', 'NO', '', '', '' 
'comm_from', 'varchar(60)', 'NO', '', '', '' 
'man_id', 'int(10) unsigned', 'NO', '', '', '' 
'supp_id', 'int(10) unsigned', 'NO', '', '', '' 
'user_id', 'int(10) unsigned', 'NO', '', '', '' 
'date_created', 'timestamp', 'NO', '', 'CURRENT_TIMESTAMP', '' 

미안하지만 세부 사항은 미안하지만 주위에 머리를 얻을 어렵다!

편집 :

select ProductLinesID, ProductID, ProductName, ProductCatalog, ManufacturerName, 
     productMSDSStatus, productStatusDesc, productStatusIcon, DATE_FORMAT(dateAdded,'%d/%m/%Y') As dateAdded, DATE_FORMAT(ProductRevision,'%d/%m/%Y') as ProductRevision, 
     ManufacturerID, SupplierName, ProductID, DATE_FORMAT(dateLatestCheck, '%d/%m/%Y') as dateLatestCheck,s.SupplierID 
     ,lastContact 
    from sds_productlines pl 
     right join sds_products p on p.ProductID = pl.productlinesProductID 
     left join sds_manufacturer m on p.ProductManufacturer = m.ManufacturerID 
     left join sds_product_status ps on p.productMSDSStatus = ps.productStatusID 
     left join sds_departments d on pl.ProductLinesDepartmentID = d.DepartmentID 
     left join sds_hospitals h on h.hospitalID = d.DepartmentHospitalID 
     left join sds_supplier s on s.SupplierID = pl.SupplierID 
     left join sds_product_comms pc on pc.prod_id = p.productID 
     left join (select comms_id, max(date_created) as lastContact from sds_comms group by comms_id) as c2 on pc.comms_id = c2.comms_id 
where productID=555; 

이이 일을 올바른 방법이 될 것이다? 이 방법을 나는 시도 같은 제품 ID :(대한

+0

같다 무엇입니까 'sds_product_comms'의 ructure? prod_id와 comms_id에서 고유합니까? – Thomas

+0

'pc_id', 'int (10) unsigned', 'NO', 'PRI', '', 'auto_increment' 'prod_id', 'int (10) unsigned', 'NO', '', '' ''comms_id ','int (10) unsigned ','NO ',' ',' ',' ' –

+0

수정 된 답변을 확인하십시오. – mtone

답변

1

을 다른 lastContact 날짜납니다이 : 내가 제대로 이해하면

select ProductLinesID, ProductID, ProductName, ProductCatalog, ManufacturerName, 
     productMSDSStatus, productStatusDesc, productStatusIcon, DATE_FORMAT(dateAdded,'%d/%m/%Y') As dateAdded, DATE_FORMAT(ProductRevision,'%d/%m/%Y') as ProductRevision, 
     ManufacturerID, SupplierName, ProductID, DATE_FORMAT(dateLatestCheck, '%d/%m/%Y') as dateLatestCheck,s.SupplierID, 
     c.date_created as lastContact 
    from sds_productlines pl 
     right join sds_products p on p.ProductID = pl.productlinesProductID 
     left join sds_manufacturer m on p.ProductManufacturer = m.ManufacturerID 
     left join sds_product_status ps on p.productMSDSStatus = ps.productStatusID 
     left join sds_departments d on pl.ProductLinesDepartmentID = d.DepartmentID 
     left join sds_hospitals h on h.hospitalID = d.DepartmentHospitalID 
     left join sds_supplier s on s.SupplierID = pl.SupplierID 
     left join sds_product_comms pc on pc.prod_id = p.productID 
     left join comms_id c on c.comms_id = pc.comms_id and c.date_created = (select max(date_created) from comms_id c2 where c2.comms_id = pc.comms_id) 
where productID=555; 
+0

귀하의 조언에 따라 답변을 편집했습니다. 진술을 확인할 수 있습니까? –

+0

많은 감사를드립니다! :) –

0

, 당신은 N에있는 테이블에있는 매개 변수를 원하는 1의 끝 :.. N 관계 및 각의 일을 갖고 싶어 당신은 GROUP BY를 사용하여 데이터를 그룹화 시도 할 수

는 뭔가 같은 :

SELECT sds_product_comms.*, MAX(sds_comms.created_at) FROM sds_product_comms AS pc 
JOIN sds_comms c ON pc.comms_id = c.comms_id 
GROUP BY sds_product_comms.prod_id 
관련 문제