动态下拉列表产生明显的结果

时间:2015-05-08 03:00:57

标签: mysql forms

我正在显示来自查询的信息,该查询基本上过滤了表单中的选项。该查询是:

`SELECT bp.*, b.company 
FROM `windows_brands_products` bp 
LEFT JOIN `windows_brands` b 
ON bp.brand_id = b.id 
JOIN Windows_last_submissions ls 
JOIN windows_materials wm 
WHERE bp.width = ROUND(ls.width) 
AND bp.height = ROUND(ls.height) 
AND bp.material IN (wm.name) 
AND bp.type = ls.type 
AND IF (ls.minimumbid != '0.00',bp.cost BETWEEN ls.minimumbid AND ls.maximumbid,bp.cost <= ls.maximumbid)`

我还从一个名为'materials_list'的数据库中的另一个表中填充一个下拉菜单,从一个视图中动态地查询:

`SELECT b.company
FROM `windows_brands_products` bp 
LEFT JOIN `windows_brands` b 
ON bp.brand_id = b.id 
JOIN Windows_last_submissions ls 
JOIN windows_materials wm 
WHERE bp.width = ROUND(ls.width) 
AND bp.height = ROUND(ls.height) 
AND bp.material IN (wm.name) 
AND bp.type = ls.type 
AND IF (ls.minimumbid != '0.00',bp.cost BETWEEN ls.minimumbid AND ls.maximumbid,bp.cost <= ls.maximumbid)`

该查询仅生成公司名称,但仍会重复。

因为我希望该列表显示上述查询中的字段“company”。所以说5个不同的公司作为该查询的一部分出现,我希望下拉列表显示这5个不同的公司。在许多情况下,查询将导致多个公司,因为同一公司的其他参数(材料,类型,成本)可能不同 - 所以我有两个或更多同一家公司出现..并填充下拉那种方式(重复一些公司名称)。如何才能使下拉列表中显示的任何公司都是唯一的?

1 个答案:

答案 0 :(得分:1)

使用group by -

SELECT b.company
FROM `windows_brands_products` bp 
LEFT JOIN `windows_brands` b ON bp.brand_id = b.id 
JOIN Windows_last_submissions ls 
JOIN windows_materials wm 
WHERE bp.width = ROUND(ls.width) 
    AND bp.height = ROUND(ls.height) 
    AND bp.material IN (wm.name) 
    AND bp.type = ls.type 
    AND IF (ls.minimumbid != '0.00',bp.cost BETWEEN ls.minimumbid AND ls.maximumbid,bp.cost <= ls.maximumbid) 
GROUP By b.company