【问题标题】:Mysql join on calculated max resultMysql加入计算的最大结果
【发布时间】:2011-03-07 17:58:46
【问题描述】:

我有一个现有的查询:

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;

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

【问题讨论】:

  • sds_product_comms的结构是什么?它在 prod_id 和 comms_id 上是唯一的吗?
  • 'pc_id', 'int(10) unsigned', 'NO', 'PRI', '', 'auto_increment' 'prod_id', 'int(10) unsigned', 'NO', '', '', '' 'comms_id', 'int(10) unsigned', 'NO', '', '', ''

标签: mysql join subquery


【解决方案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;

【讨论】:

  • 我已根据您的建议编辑了答案,您可以检查一下声明吗?
【解决方案2】:

如果我理解正确,您需要一个位于 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-23
    相关资源
    最近更新 更多