【发布时间】: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', '', '', ''