Database Reference
In-Depth Information
interesting sender might be postmaster or root . Those names don't appear in the table,
so let's use phil as the name of interest instead:
mysql> SELECT t, srcuser, dstuser, size
-> FROM mail
-> ORDER BY IF(srcuser='phil',0,1), srcuser, dstuser;
+---------------------+---------+---------+---------+
| t | srcuser | dstuser | size |
+---------------------+---------+---------+---------+
| 2014-05-16 23:04:19 | phil | barb | 10294 |
| 2014-05-12 15:02:49 | phil | phil | 1048 |
| 2014-05-15 08:50:57 | phil | phil | 978 |
| 2014-05-14 11:52:17 | phil | tricia | 5781 |
| 2014-05-19 12:49:23 | phil | tricia | 873 |
| 2014-05-14 14:42:21 | barb | barb | 98151 |
| 2014-05-11 10:15:08 | barb | tricia | 58274 |
| 2014-05-12 18:59:18 | barb | tricia | 271 |
| 2014-05-14 09:31:37 | gene | barb | 2291 |
| 2014-05-16 09:00:28 | gene | barb | 613 |
| 2014-05-15 17:35:31 | gene | gene | 3856 |
| 2014-05-15 07:17:48 | gene | gene | 3824 |
| 2014-05-19 22:21:51 | gene | gene | 23992 |
| 2014-05-15 10:25:52 | gene | tricia | 998532 |
| 2014-05-12 12:48:13 | tricia | gene | 194925 |
| 2014-05-14 17:03:01 | tricia | phil | 2394482 |
+---------------------+---------+---------+---------+
The value of the extra sort column is 0 for rows in which the srcuser value is phil , and
1 for all other rows. By making that the most significant sort column, rows for messages
sent by phil float to the top of the output. (To sink them to the bottom instead, either
sort the column in reverse order using DESC , or reverse the order of the second and third
arguments of the IF() function.)
You can also use this technique for particular conditions, not only specific values. To
put first those rows where people sent messages to themselves, do this:
mysql> SELECT t, srcuser, dstuser, size
-> FROM mail
-> ORDER BY IF(srcuser=dstuser,0,1), srcuser, dstuser;
+---------------------+---------+---------+---------+
| t | srcuser | dstuser | size |
+---------------------+---------+---------+---------+
| 2014-05-14 14:42:21 | barb | barb | 98151 |
| 2014-05-19 22:21:51 | gene | gene | 23992 |
| 2014-05-15 17:35:31 | gene | gene | 3856 |
| 2014-05-15 07:17:48 | gene | gene | 3824 |
| 2014-05-12 15:02:49 | phil | phil | 1048 |
| 2014-05-15 08:50:57 | phil | phil | 978 |
| 2014-05-11 10:15:08 | barb | tricia | 58274 |
| 2014-05-12 18:59:18 | barb | tricia | 271 |
| 2014-05-16 09:00:28 | gene | barb | 613 |
| 2014-05-14 09:31:37 | gene | barb | 2291 |
Search WWH ::




Custom Search