SELECT * FROM people WHERE user_id ='$ user_id'ORDER BY time GROUP BY surname [英] SELECT * FROM people WHERE user_id='$user_id' ORDER BY time GROUP BY surname

查看:169
本文介绍了SELECT * FROM people WHERE user_id ='$ user_id'ORDER BY time GROUP BY surname的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要提取数据库中与某个user_id相关的所有条目,在它们输入数据库之前(这是php unix time()秒),然后按姓氏对它们进行分组,以便他们坐在同一姓氏的条目中。

这是迄今为止的查询。尽管它只给了我一定数量的条目。

  $ sql =SELECT * FROM people WHERE user_token ='$ user_token'
ORDER BY time GROUP BY surname;

使用ORDER和GROUP by的正确方法是将所有结果分组由姓氏,但在进入时的命令?感谢。 您必须指定要汇总的字段。



例如:

  SELECT surname,count(*)
FROM people
WHERE user_token ='$ user_token'
GROUP BY姓氏
ORDER BY姓氏


I need to pull out all the entries in my database that relate to a certain user_id, order them by the time they were entered into the database (this is php unix time() seconds) and then group them by surname so that they sit with entries of the same surname.

This is the query so far.. Although it only gives me a certain amount of entries.

$sql = "SELECT * FROM people WHERE user_token='$user_token'
        ORDER BY time GROUP BY surname";

What is the correct way to use ORDER and GROUP by in order to get all of the results out grouped by the surname but ordered by the time the were entered? Thanks.

解决方案

The ORDER BY clause should be the last, but you have to specify fields to be aggregated.

Such as:

SELECT surname, count(*) 
    FROM people 
    WHERE user_token='$user_token'
    GROUP BY surname
    ORDER BY surname

这篇关于SELECT * FROM people WHERE user_id ='$ user_id'ORDER BY time GROUP BY surname的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆