Mysql连接计算的最大结果

时间:2011-03-07 17:58:46

标签: mysql join subquery

我有一个现有的查询:

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行具有相同的最大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;

这是一种正确的做法吗?使用这种方法,我得到相同productID的不同lastContact日期:(

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

如果我理解正确,您需要一个表格中的参数,该表格位于1:N关系的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