简体   繁体   English

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

[英]using MAX and COUNT together in mysql query

I'm new in mysql. 我是mysql的新手。 I have 2 mysql tables, company and info . 我有2个mysql表, companyinfo The 1st table has cid, name, classification, address, city, phone, province and the 2nd table has cid, information dan infodate . 第一个表具有cid,名称,分类,地址,城市,电话,省 ,第二个表具有cid,信息和infodate They are relational where company.cid=info.cid. 它们是company.cid = info.cid的关系。 I have used 我用过

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

I want to add COUNT in that statement so each company will have sum of informations to display. 我想在该语句中添加COUNT ,以便每个公司都有要显示的信息总和。 Thank you. 谢谢。

Try this 尝试这个

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

And then, to retrieve the count use 然后,检索计数使用

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

Try this 尝试这个

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

如果执行此操作会发生什么:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM