在下面的虚构示例中,有(主)表country_sales。
在每个添加的记录中可以添加几辆车和几个无线电。
我需要对搜索/计数组件进行累积查询。
if ($group == 'cars'){
$query = "SELECT COUNT (cs.id_cars) AS total, tc.desc_cars AS isname
FROM cars_sold cs, tab_cars tc, country_sales csa
WHERE csa.status = 1
AND tc.id_cars = cs.id_cars
AND csa.id_name = tc.id_name ";
}
if ($group == 'extras'){
$query = "SELECT COUNT (es.id_radio) AS total, tc.desc_radio AS isname
FROM radio_sold es, tab_radio tex, country_sales csa
WHERE csa.status = 1
AND tex.id_radio = es.id_radio
AND csa.id_name = tex.id_name ";
}
if($cars > 0 ){
$query .= " AND csa.id_name IN(SELECT id_name FROM tab_cars WHERE id_cars = ? )";
}
if($extras > 0 ){
$query .= " AND csa.id_name IN(SELECT id_name FROM tab_extras WHERE id_extras = ? )";
}
$query .= " GROUP BY isname";
如果选项是小组(由)汽车而且汽车是福特野马,我不仅可以获得销售的福特野马汽车的数量,还可以获得所有其他车型的清单。
出售的无线电也是如此。
如果查询累计汽车和收音机工作但仍显示所有汽车或所有无线电按照GROUP BY
标准销售的列表我尝试实现的只是获得具有所需计数的一行。例如,只有80辆福特野马。
我尝试了以下内容:
if ($group == 'cars'){
$query = "SELECT COUNT (cs.id_cars) AS total, tc.desc_cars AS isname
FROM cars_sold cs
LEFT JOIN tab_cars tc ON cs.id_cars = tc.id_cars
LEFT JOIN country_sales csa ON tc.id_name = csa.id_name
WHERE csa.status = 1 ";
}
if ($group == 'extras'){
$query = "SELECT COUNT (es.id_radio) AS total, tc.desc_radio AS isname
FROM radio_sold es
LEFT JOIN tab_radio tex ON es.id_radio = tex.id_radio
LEFT JOIN country_sales csa ON tex.id_name = csa.id_name
country_sales csa
WHERE csa.status = 1 ";
}
if($cars > 0 ){
$query .= " AND id_cars = ? ";
}
if($extras > 0 ){
$query .= " AND id_extras = ? ";
}
$query .= " GROUP BY isname";
问题:
如果我通过汽车或收音机进行搜索,我会得到一行所需的结果。
然而,汽车和收音机的累积查询不起作用。
任何想法如何解决?