2

PHP - How to use ORDER BY and GROUP BY together

 2 years ago
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

advertisements

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 BYsender' 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?

I use Cassandra 3.0.5. I'm having problem using ORDER BY and IN together. Schema: CREATE TABLE my_status.user_status_updates ( username text, id timeuuid, body text, PRIMARY KEY (username, id)) WITH CLUSTERING ORDER BY (id ASC) AND bloom_filter_fp_ch

How to use getElementsByClassName, onchange and this.value together?

I'm trying to check multiple file upload values for an image type. With this html: <input type="file" name="image" id="fileUpload"> And this js: document.getElementById('fileUpload').onchange = function () { var filenam

how to use multiple sum and group by in subquery?

ER DIAGRAM SNAP have to Find the Order Amount Total for 2 type of orders for each year. table is SalesOrderHeader Subtotal + TaxAmt gives the total order amount OnlineOrderFlag(0/1 is direct/online respectively) 1>Display the results in the below for

Use Order By and Group by a Union result of 2 different columns

This My Table ID Value SenderID RecieverID 1 Hello There 2 7 2 etc etc 7 5 3 etc 2 6 4 ee 7 2 5 asdas 2 7 6 asdas 2 5 7 asdas 7 5 What I want is the value from either senderID or receiverID from all the rows in which a specific value let say 2 occurs

How to use ng-if and ng-blur together?

<input kendo-time-picker name="ArriveDateTime" class="form-control" k-format="'hh:mm tt'" k-parse-formats="['hh:mm tt']" k-ng-model="technician.ArriveDateTime" k-ng-disabled="!technician.IsLocked&q

Using the case and grouping function to substitute the literal value for summary rows with null values

I have a sql code here I've made and I am trying to figure out how to use the case and grouping function to substitute the literal values "ALL" for the summary rows with null values. I also want to filter results with states ending with AZ, MI,

MySql: order by and group by combining not to give the last record

Using the following query I am looking for a solution to get the latest record that having some conditions. But it gives me the first record, not the latest. I think its only considering the group by Please advise me SELECT * FROM `contacts` WHERE `i

How to use the single column group in MSSQL

I have a query written for MySql that uses the LIMIT and GROUP BY keywords. It works on fine on MySql. The query is: SELECT tester, firstname, COUNT(*) total FROM mnrelease_details a LEFT JOIN users b ON tester='guest' WHERE a.project='testpro' GROUP

How can I get user and group information in Perl on Windows?

Perl has several built-in functions for accessing /etc/passwd on Unix systems (and elsewhere when supported) for user and group information. For instance, my $name = getpwuid($uid); will return the user name given the user ID, or undef if there is no

How to use the .key and .crt file in java generated by openssl?

I need asymmetric encryption in java. I generate .key and .crt files with own password and .crt file by openssl that said in http://www.imacat.idv.tw/tech/sslcerts.html . How to use these .key and .crt file to extract publickey and private key in Jav

How to use ORDER BY CHARINDEX () with UNION?

How to use ORDER BY CHARINDEX() with UNION? What I want is like: select Id,Name from A where Name like '%Raspberry%' Union select Id,Name from B where Name like '%Raspberry%' order by CHARINDEX('Raspberry',Name) Common Table Expression will help you:

How to use an alias and add them in a select query

I have the following queries below. I'd like to use the aliases and add them together but SQL does not allow that. Any suggestions on how I can go about this without repeating the queries again? Using Microsoft SQL Server 2008 SELECT SUM(CASE WHEN rg

How to use jquery Show and hide for this job

How to use jquery show and hide for this because. I know javascript I have implemented that.How to do that in jquery. If I clicked monday,tuesday and wednesday. I am showing the monday and tuesday timing form. Assume If I clicked monday. below monday

Use the JSON and JSONP format together in the YII2 REST API

I'm new in yiiframework. I want use JSONP and JSON format together in Yii2 REST api, it's means if a api call normal, response get json and if a api call with jsonp ajax, get JSONP result, I can't find anything in forums or stackoverflow to config th

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK