Home >> FAQs/Tutorials >> MySQL Tutorials >> Index

MySQL FAQs - Understanding SQL INSERT, UPDATE and DELETE Statements

By: FYIcenter.com

Part:   1  2  3  4  5   6  7 

(Continued from previous part...)

How To Use Values from Other Tables in UPDATE Statements?

If you want to update values in one table with values from another table, you can use a subquery as an expression in the SET clause. The subquery should return only one row for each row in the update table that matches the WHERE clause. The tutorial exercise below shows you a good example:

mysql> CREATE TABLE fyi_rates (id INTEGER, 
   comment VARCHAR(16));
Query OK, 0 rows affected (0.53 sec)

mysql> INSERT INTO fyi_rates VALUES (101, 'The best');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO fyi_rates VALUES (102, 'Well done');
Query OK, 1 row affected (0.01 sec)

mysql> INSERT INTO fyi_rates VALUES (103, 'Thumbs up');
Query OK, 1 row affected (0.00 sec)

mysql> UPDATE fyi_links SET notes = (
    SELECT comment FROM fyi_rates 
    WHERE fyi_rates.id = fyi_links.id
   )
   WHERE id > 0 AND id < 110;
Query OK, 3 rows affected (0.50 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> SELECT id, url, notes, counts FROM fyi_links 
   WHERE id > 0 AND id < 110;
+-----+-------------------+-----------+--------+
| id  | url               | notes     | counts |
+-----+-------------------+-----------+--------+
| 101 | dev.fyicenter.com | The best  |    999 |
| 102 | dba.fyicenter.com | Well done |      0 |
| 103 | sqa.fyicenter.com | Thumbs up |   NULL |
+-----+-------------------+-----------+--------+
3 rows in set (0.00 sec)

Note that if column names are confusing between the inner table and the outer table, you need to prefix column names with table names, like "fyi_rates.id = fyi_links.id".

What Happens If the UPDATE Subquery Returns No Rows?

If you use a subquery to assign new values in the SET clause in an UPDATE statement, and the subquery returns no rows for an outer row, MySQL will provide a NULL value to the SET clause. The tutorial exercise below shows you a good example:

mysql> UPDATE fyi_links SET notes = 'Number one' 
   WHERE id = 0;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> SELECT id, url, notes, counts FROM fyi_links 
   WHERE id = 0;
+----+-------------------+------------+--------+
| id | url               | notes      | counts |
+----+-------------------+------------+--------+
|  0 | www.fyicenter.com | Number one |   NULL |
+----+-------------------+------------+--------+
1 row in set (0.00 sec)

mysql> SELECT * FROM fyi_rates WHERE id = 0;
Empty set (0.00 sec)

mysql> UPDATE fyi_links SET notes = (
    SELECT comment FROM fyi_rates 
    WHERE fyi_rates.id = fyi_links.id
   ) 
   WHERE id = 0;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> SELECT id, url, notes, counts FROM fyi_links 
   WHERE id = 0;
+----+-------------------+-------+--------+
| id | url               | notes | counts |
+----+-------------------+-------+--------+
|  0 | www.fyicenter.com | NULL  |   NULL |
+----+-------------------+-------+--------+
1 row in set (0.00 sec)

(Continued on next part...)

Part:   1  2  3  4  5   6  7 


Selected Developer Jobs:

More...