MySQL更新命令未生效:匹配行无变更问题求助
Troubleshooting Your Date Format Update Issue
Let's break down why your update isn't having any effect and walk through the fixes to get the date format you want.
Why Your Update Isn't Working
You're seeing "0 row(s) affected" for a few key reasons:
- Wrong format specifier in
DATE_FORMAT
You're using%Y/%m/%dwhich outputs dates like2017/05/30, but your goal is20170530. Even worse, if yourdatecolumn is aDATEorDATETIMEtype, MySQL will automatically convert that slash-separated string back to the standardYYYY-MM-DDstorage format—so the value doesn't actually change. - Mismatched
WHEREclause or pre-existing values
TheWHERE counter = 1condition might not be targeting the rows you think it is, or those rows already have a value that MySQL considers identical to what you're trying to set (thanks to the automatic type conversion mentioned above). DATEcolumns store fixed formats
If yourdatecolumn is a nativeDATEtype, MySQL stores it in an internal format (not the human-readableYYYY-MM-DDyou see) and only formats it asYYYY-MM-DDwhen querying. You can't modify the storage format directly—you can only control how it's displayed when you fetch the data.
Fixes to Resolve the Issue
Choose the solution that fits your actual need:
Option 1: Just format dates when querying (no table changes needed)
If you don't need to change the stored data, just use DATE_FORMAT in your SELECT queries to get the YYYYMMDD format:
SELECT DATE_FORMAT(date, '%Y%m%d') AS formatted_date, counter, ... FROM stations.attenuation_smoothed;
Option 2: Change the column type and update the values
If you must store the date as a string in YYYYMMDD format:
- First disable safe updates (to avoid errors with the
ALTERandUPDATEcommands):SET SQL_SAFE_UPDATES = 0; - Modify the
datecolumn to a string type (likeVARCHAR(8)sinceYYYYMMDDis 8 characters):ALTER TABLE stations.attenuation_smoothed MODIFY COLUMN date VARCHAR(8); - Update the values to the desired format:
(TheUPDATE stations.attenuation_smoothed SET date = DATE_FORMAT(STR_TO_DATE(date, '%Y-%m-%d'), '%Y%m%d') WHERE counter = 1;STR_TO_DATEensures we correctly parse the existingYYYY-MM-DDvalue before reformatting it.) - Re-enable safe updates:
SET SQL_SAFE_UPDATES = 1;
Option 3: Verify your WHERE clause is correct
Double-check that you're targeting the right rows by running a SELECT first:
SELECT * FROM stations.attenuation_smoothed WHERE counter = 1;
This will show you if there are rows matching the condition, and what their current date values are.
内容的提问来源于stack exchange,提问作者Muhammad Ahsan Gillani Gillani
相关产品推荐
相关产品推荐

