PHP - How to use ORDER BY and GROUP BY together
source link: https://www.codesd.com/item/php-how-to-use-order-by-and-group-by-together.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
PHP - How to use ORDER BY and GROUP BY together
I created a table that contains message, sender, to, time
I want group by sender and order by time this is my code
$query= mysql_query("SELECT * FROM `table` ORDER BY `time` GROUP BY `sender`")or die(mysql_error());
while($arr = mysql_fetch_array($query)){
$num = mysql_num_rows($query);
$msg = $arr ['message'];
echo '</br>';
echo $msg;
}
that shows me this error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP BY
sender' at line 1
So how to fix that problem?
Thanks
Klaus
try this code
$query= mysql_query("SELECT * FROM `table` GROUP BY `sender` ORDER BY `time` ")or die(mysql_error());
// ^^--will be before order
$num = mysql_num_rows($query); // out of the while loop
while($arr = mysql_fetch_array($query)){
$msg = $arr['message'].'<br />';
// ^^--remove space here
echo $msg;
}
Related Articles
How to use ORDER BY and IN together in a query in Cassandra?
How to use getElementsByClassName, onchange and this.value together?
how to use multiple sum and group by in subquery?
Use Order By and Group by a Union result of 2 different columns
How to use ng-if and ng-blur together?
Using the case and grouping function to substitute the literal value for summary rows with null values
MySql: order by and group by combining not to give the last record
How to use the single column group in MSSQL
How can I get user and group information in Perl on Windows?
How to use the .key and .crt file in java generated by openssl?
How to use ORDER BY CHARINDEX () with UNION?
How to use an alias and add them in a select query
How to use jquery Show and hide for this job
Use the JSON and JSONP format together in the YII2 REST API
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK