2011-03-07 61 views
1

加入我有一个现有的查询: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 

这是在一个PHP页面定义。我被要求添加另一个存储在另一个表中的参数,问题是,在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做到这一点,所以是有可能以某种方式使一个子查询坚持这两个在一起,而不会在最初的查询中创建重复的行。

例如,我不希望5行的相同产品具有相同的max date_created。

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; 

这会不会做这件事的正确方法吗?用这种方法我得到不同的lastContact日期相同:(的productID

+0

什么是ST 'sds_product_comms'的结构?它在prod_id和comms_id上是唯一的吗? – Thomas 2011-03-07 18:32:26

+0

'pc_id','int(10)unsigned','NO','PRI','','auto_increment' 'prod_id','int(10)unsigned','NO','','' '' 'comms_id','int(10)unsigned','NO','','','' – 2011-03-07 18:55:05

+0

检查我编辑的答案。 – mtone 2011-03-07 19:37:22

回答

1

试试这个:

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

我根据你的建议编辑了答案,你能检查一下陈述吗? – 2011-03-07 18:51:02

+0

非常感谢! :) – 2011-03-07 21:44:39

0

如果我理解正确的话,你想有一个参数是一个表,它是在N的1结束:。N的关系,你想只有每1

您可以尝试使用分组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