To put first those records where people sent messages to themselves : Where « Where Clause « SQL / MySQL






To put first those records where people sent messages to themselves

       
mysql>
mysql> CREATE TABLE mail
    -> (
    ->  t               DATETIME,       # when message was sent
    ->  senderUser      CHAR(8),        # sender (source user and host)
    ->  senderHost      CHAR(20),
    ->  recipientUser   CHAR(8),        # recipient (destination user and host)
    ->  recipientHost   CHAR(20),
    ->  size    BIGINT,         # message size in bytes
    ->  INDEX   (t)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> INSERT INTO mail (t,senderHost,senderUser,recipientHost,recipientUser,size)
    ->  VALUES
    ->          ('2010-05-11 10:15:08','saturn','barb','mars','tricia',58274),
    ->          ('2010-05-12 12:48:13','mars','tricia','venus','gene',194925),
    ->          ('2010-05-12 15:02:49','mars','phil','saturn','phil',1048),
    ->          ('2010-05-13 13:59:18','saturn','barb','venus','tricia',271),
    ->          ('2010-05-14 09:31:37','venus','gene','mars','barb',2291),
    ->          ('2010-05-14 11:52:17','mars','phil','saturn','tricia',5781),
    ->          ('2010-05-14 14:42:21','venus','barb','venus','barb',98151),
    ->          ('2010-05-14 17:03:01','saturn','tricia','venus','phil',2394482),
    ->          ('2010-05-15 07:17:48','mars','gene','saturn','gene',3824),
    ->          ('2010-05-15 08:50:57','venus','phil','venus','phil',978),
    ->          ('2010-05-15 10:25:52','mars','gene','saturn','tricia',998532),
    ->          ('2010-05-15 17:35:31','saturn','gene','mars','gene',3856),
    ->          ('2010-05-16 09:00:28','venus','gene','mars','barb',613),
    ->          ('2010-05-16 23:04:19','venus','phil','venus','barb',10294),
    ->          ('2010-05-17 12:49:23','mars','phil','saturn','tricia',873),
    ->          ('2010-05-19 22:21:51','saturn','gene','venus','gene',23992);
Query OK, 16 rows affected (0.00 sec)
Records: 16  Duplicates: 0  Warnings: 0

mysql>
mysql>
mysql> SELECT t, senderUser, recipientUser, size
    -> FROM mail
    -> ORDER BY IF(senderUser=recipientUser,0,1), senderUser, recipientUser;
+---------------------+------------+---------------+---------+
| t                   | senderUser | recipientUser | size    |
+---------------------+------------+---------------+---------+
| 2010-05-14 14:42:21 | barb       | barb          |   98151 |
| 2010-05-19 22:21:51 | gene       | gene          |   23992 |
| 2010-05-15 17:35:31 | gene       | gene          |    3856 |
| 2010-05-15 07:17:48 | gene       | gene          |    3824 |
| 2010-05-12 15:02:49 | phil       | phil          |    1048 |
| 2010-05-15 08:50:57 | phil       | phil          |     978 |
| 2010-05-11 10:15:08 | barb       | tricia        |   58274 |
| 2010-05-13 13:59:18 | barb       | tricia        |     271 |
| 2010-05-16 09:00:28 | gene       | barb          |     613 |
| 2010-05-14 09:31:37 | gene       | barb          |    2291 |
| 2010-05-15 10:25:52 | gene       | tricia        |  998532 |
| 2010-05-16 23:04:19 | phil       | barb          |   10294 |
| 2010-05-14 11:52:17 | phil       | tricia        |    5781 |
| 2010-05-17 12:49:23 | phil       | tricia        |     873 |
| 2010-05-12 12:48:13 | tricia     | gene          |  194925 |
| 2010-05-14 17:03:01 | tricia     | phil          | 2394482 |
+---------------------+------------+---------------+---------+
16 rows in set (0.00 sec)

mysql>
mysql> drop table mail;
Query OK, 0 rows affected (0.00 sec)

   
    
    
    
    
    
    
  








Related examples in the same category

1.Not equal in where
2.Getting the List of Products That Are on Catalog Promotion
3.Using Where Conditions
4.WHERE Clause Comparisons
5.Combining WHERE Conditions
6.Calculation in WHERE clause
7.Compare and calculate in Where clause
8.Do Calculation in Where and order
9.Where clause: nested conditions
10.Where clause: calculation and equal condition
11.Where clause: compare
12.Where clause: XOR
13.Use where clause the narrow down results
14.Use CURRENT_DATE in where clause
15.WHERE TRUE OR FALSE
16.Change localhost to the name of the machine where you'll be working.
17.WHERE clauses can test multiple conditions.
18.Using a WHERE clause that matches up values in the author ID column
19.Add a WHERE clause that looks for NULL values in the book column that is named in the ON clause
20.Add an expression to the WHERE clause that explicitly excludes the reference
21.Adds an additional condition to the WHERE clause and the calculated columns must have a total greater than 20
22.A WHERE clause that contains two expressions (conditions)
23.Searches for surnames that have an e anywhere in the name and then end with an e:
24.Show all records where the color is "Silver" and the price is above 100.00
25.Show records where make is "Krups", "Gaggia" or "DeLonghi" and the model is not "TSK-182" or "EC410"
26.Show all records where the color is not "Silver" or the price is below 100.00
27.Get the number of items for each color where the price exceeds 150.00 and when there is more than 1 item for t
28.Check day name in where clause
29.Compare the results from two statements