Collections:
Order of Columns in the SET Clause in MySQL
Is the Order of Columns in the SET Clause Important in MySQL?
✍: FYIcenter.com
Yes. The order of columns in the SET clause of the UPDATE statement is important. There is a BIG DIFFERENCE between MySQL and Oracle on update columns with previous values:
Tutorial exercise 1 - Setting "id" first and "counts" second:
mysql> SELECT id, url, notes, counts, DATE(created) FROM fyi_links WHERE url = 'dev.fyicenter.com'; +-----+-------------------+-------+--------+---------------+ | id | url | notes | counts | DATE(created) | +-----+-------------------+-------+--------+---------------+ | 101 | dev.fyicenter.com | Good. | 999 | 2006-04-30 | +-----+-------------------+-------+--------+---------------+ 1 row in set (0.00 sec) mysql> UPDATE fyi_links SET id = id+200, counts = id*2 WHERE url = 'dev.fyicenter.com'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> SELECT id, url, notes, counts, DATE(created) FROM fyi_links WHERE url = 'dev.fyicenter.com'; +-----+-------------------+-------+--------+---------------+ | id | url | notes | counts | DATE(created) | +-----+-------------------+-------+--------+---------------+ | 301 | dev.fyicenter.com | Good. | 602 | 2006-04-30 | +-----+-------------------+-------+--------+---------------+ 1 row in set (0.00 sec) mysql> UPDATE fyi_links SET id = 101, counts = 999 WHERE url = 'dev.fyicenter.com'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
Notice that the "id" in the "counts" new value expression is taking the new value of the "id" column, because it appears before the "counts" columns in the SET clause.
Tutorial exercise 2 - Setting "counts" first and "id" second:
mysql> SELECT id, url, notes, counts, DATE(created) FROM fyi_links WHERE url = 'dev.fyicenter.com'; +-----+-------------------+-------+--------+---------------+ | id | url | notes | counts | DATE(created) | +-----+-------------------+-------+--------+---------------+ | 101 | dev.fyicenter.com | Good. | 999 | 2006-04-30 | +-----+-------------------+-------+--------+---------------+ 1 row in set (0.00 sec) mysql> UPDATE fyi_links SET counts = id*2, id = id+200 WHERE url = 'dev.fyicenter.com'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> SELECT id, url, notes, counts, DATE(created) FROM fyi_links WHERE url = 'dev.fyicenter.com'; +-----+-------------------+-------+--------+---------------+ | id | url | notes | counts | DATE(created) | +-----+-------------------+-------+--------+---------------+ | 301 | dev.fyicenter.com | Good. | 202 | 2006-04-30 | +-----+-------------------+-------+--------+---------------+ 1 row in set (0.00 sec) mysql> UPDATE fyi_links SET id = 101, counts = 999 WHERE url = 'dev.fyicenter.com'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
Notice that the "id" in the "counts" new value expression is taking the old value of the "id" column, because it appears after the "counts" columns in the SET clause.
⇒ UPDATE Using Data from Other Tables in MySQL
⇐ Use Existing Column Values in the SET Clause in MySQL
2018-01-13, 1489🔥, 0💬
Popular Posts:
How to run Queries with SQL Server Management Studio Express in SQL Server? 1. Launch and connect SQ...
How To Convert a Unicode Strings to Non-Unicode Strings in SQL Server Transact-SQL? Since Unicode ch...
How To Look at the Current SQL*Plus System Settings in Oracle? If you want to see the current values...
How To Query Tables and Loop through the Returning Rows in MySQL? The best way to query tables and l...
Where to find answers to frequently asked questions on INSERT, UPDATE and DELETE Statements in MySQL...