在这样的查询中:
SELECT GROUP_CONCAT(DISTINCT( p.products_id )) AS comma_separated
FROM products p
left join specials s
ON p.products_id = s.products_id
left join products_to_categories p2c
ON p.products_id = p2c.products_id
left join products_description pd
ON p.products_id = pd.products_id
inner join products_attributes pa
ON p.products_id = pa.products_id
WHERE p.products_status = '1'
AND p2c.categories_id = 57
AND Date_sub(Curdate(), interval 7000 day) <= p.products_date_added
ORDER BY p.products_id DESC
我在名为comma_separated的字段中获得了逗号分隔数字的长列表结果。 如何修改查询以计算有多少逗号分隔值?
修改
谢谢,Barmar的答案如下。 我继续得到一个更短的数字列表(107个值而不是实际计数中的4000+),现在我终于找到了原因。 似乎GROUP_CONCAT限制为1024个字符,这对我的应用程序来说非常糟糕。 资料来源:http://www.bennadel.com/blog/2669-mysql-group-concat-fails-silently-when-it-hits-its-size-limit.htm
答案 0 :(得分:4)
使用COUNT
来计算它们。
SELECT Group_concat(DISTINCT( p.products_id )) AS comma_separated,
COUNT(DISTINCT p.products_id) AS product_count
FROM ...