Abe Petrillo
Abe Petrillo

Reputation: 2447

Mysql join on calculated max result

I have an existing query:

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

which is defined in a php page. I've been asked to add another parameter which is stored in another table, the problem is, on sds_product can have many "communications" which is basicly like a comment with a date_created. For example, if I wanted a list of communications for a given product, I would do:

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

I wanted to do this directly in SQL, so is it possible to somehow make a sub query to stick these two together, without creating duplicate rows in the initial query.

For example I don't want 5 rows of the same product with the same 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', ''

Sorry for the detail but its hard to get my head around it!

EDIT:

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;

would this be a correct way of doing it? With this method I'm getting different lastContact dates for the same productID :(

Upvotes: 1

Views: 260

Answers (2)

mtone
mtone

Reputation: 1787

Try this:

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;

Upvotes: 1

Tomas
Tomas

Reputation: 714

If I understand you correctly, you want a parameter that is in a table that is on the N end of a 1:N relationship and you want to have only 1 of each.

You can try grouping your data using GROUP BY. Something like:

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

Upvotes: 0

Related Questions