Home >Database >Mysql Tutorial >How to Update a Specific Range of Rows in MySQL and Handle NULL Values?

How to Update a Specific Range of Rows in MySQL and Handle NULL Values?

Linda Hamilton
Linda HamiltonOriginal
2024-12-04 03:40:12503browse

How to Update a Specific Range of Rows in MySQL and Handle NULL Values?

MySQL: Updating Rows with LIMIT

Updating a specific range of rows in MySQL can be achieved using a subquery in conjunction with your UPDATE statement. The syntax error you encountered in your initial query stems from incorrectly specifying the limit clause.

To update rows from 1001 to the next 1000, use the following construct:

UPDATE table_name
SET p_id = 3
WHERE id IN (
    SELECT id
    FROM (
        SELECT id
        FROM table_name
        ORDER BY id ASC
        LIMIT 1000, 1000
    ) tmp
)

Regarding the issue with updating NULL values, your query:

UPDATE table_name SET p_id = 3 WHERE p_id = null

is not correct because MySQL treats NULL as a distinct value. To update NULL values, use the IS NULL comparison operator:

UPDATE table_name SET p_id = 3 WHERE p_id IS NULL

The above is the detailed content of How to Update a Specific Range of Rows in MySQL and Handle NULL Values?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn