在mysql查询中一起使用MAX和COUNT

时间:2011-08-08 02:34:20

标签: mysql

我是mysql的新手。我有2个mysql表,公司信息。第一个表有 cid,名称,分类,地址,城市,电话,省,第二个表有 cid,信息dan infodate 。它们是company.cid = info.cid的关系。我用过

SELECT *, MAX(infodate)
FROM company
JOIN info ON company.cid = info.cid
WHERE classification = 'Hotel'
GROUP BY name
ORDER BY MAX(infodate) DESC
LIMIT 0,7

我想在该声明中添加 COUNT ,以便每个公司都有要显示的信息总和。谢谢。

3 个答案:

答案 0 :(得分:0)

试试这个

SELECT SQL_CALC_FOUND_ROWS *, MAX(infodate)
FROM company
JOIN info ON company.cid = info.cid
WHERE classification = 'Hotel'
GROUP BY name
ORDER BY MAX(infodate) DESC
LIMIT 0,7

然后,要检索计数使用

$total = mysql_fetch_row(mysql_query("SELECT FOUND_ROWS()"));
$total = $total[0];

答案 1 :(得分:0)

试试这个

SELECT company.*, COUNT(info.information) 
    FROM company JOIN info ON company.cid = info.cid
    WHERE classification = 'Hotel'
    GROUP BY company.cid
    ORDER BY MAX(info.infodate) DESC
    LIMIT 0,7

答案 2 :(得分:-1)

如果你这样做会发生什么:

SELECT *, MAX(infodate), COUNT(*) as count