1
SELECT t.id_type, brand, model, countMod FROM types t  
    LEFT JOIN (
       SELECT p.id_type, brand, model, COUNT( model ) AS countMod 
           FROM products p  
           GROUP BY brand) p2 
    ON t.id_type = p2.id_type 
    WHERE t.id_type = "TCE" 
    ORDER BY brand

此查询工作正常,但我丢失了分组的模型,当然,在每个品牌下。
有没有办法列出和计算模型?
即brand1,model1,model2,model3,countMod=3——brand2,model1,model2,countMod=2。
任何帮助将不胜感激。谢谢

4

3 回答 3

2
SELECT brand, GROUP_CONCAT(model) models, COUNT(model) AS countMod
FROM products
GROUP BY brand
于 2013-01-19T08:06:45.360 回答
1

试试这个::

SELECT 
t.id_type, 
brand,
GROUP_CONCAT(model SEPARATOR ',') as model, 
COUNT( model ) AS countMod 
FROM types t
LEFT JOIN products p  ON t.id_type = p.id_type
WHERE t.id_type = "TCE" 
GROUP BY brand
ORDER BY brand
于 2013-01-19T08:03:07.867 回答
-1

推荐的方法是运行两个查询,一个用于数据,一个用于COUNT().

使用 MySQL,您也可以使用SQL_CALC_FOUND_ROWS(主要是如果您有一个LIMIT子句),但是它的性能是有争议的,请参阅哪个最快?SELECT SQL_CALC_FOUND_ROWS FROM `table`,或 SELECT COUNT(*)

于 2013-01-19T08:02:34.720 回答