我正在尝试在查询中应用COUNT(),我让它像这样工作:
$sql = "SELECT id, Title, images, recdate, 'item' AS type FROM ads_list WHERE to_days(now())<=(to_days(recdate)+14)";
我试过了:
1. $sql = "SELECT id, Title, images, recdate, 'item' AS type COUNT(*) FROM ads_list WHERE to_days(now())<=(to_days(recdate)+14)";
2. $sql = "SELECT COUNT(*) id, Title, images, recdate, 'item' AS type FROM ads_list WHERE to_days(now())<=(to_days(recdate)+14)";
3. $sql = "SELECT COUNT(id), Title, images, recdate, 'item' AS type FROM ads_list WHERE to_days(now())<=(to_days(recdate)+14)";
4. $sql = "SELECT id, Title, images, recdate, 'item' AS type FROM ads_list WHERE to_days(now())<=(to_days(recdate)+14) COUNT(*)";
我做错了什么?
答案 0 :(得分:1)
Count(*)是按功能分组,它返回所选行的计数。对于未包含在group by子句中的任何列,您必须具有按功能分组。所以,你可以这样做:
SELECT count(*) as 'Count' FROM ads_list WHERE ...
但您不能(直接)按功能和四个标准列选择一个组。你想用计数(*)做什么?
对于总行数,您可以使用mysql_num_rows($result_resource)
。如果您需要行和计数(并且您使用的是标准的mysql库),这可能是获得所需内容的最佳方式。如果您不需要行(仅计数),则使用count(*)。