I have an UPDATE and SELECT statement working in SQL however I am unable to replicate the same outcome in MYSQL.
I have a table (TBL1) with, say 4 columns:-
col1: registration (VARCHAR)
col2: date
col3: oldest date flag (values Y or blank)
col4: index (priamry key - auto-increment).
An ongoing process continues to add registrations and dates to the table. After a batch of additions any col3 'Y' flags will be removed and then an update process will set the col3 value = 'Y' for each the row where the registration has the oldest date.
Here is the SQL code, this works as expected after an earlier process removes any previous col3 'Y' values;-
UPDATE T1
SET T1.Col3 = 'Y'
SELECT FROM TBL1 AS T1
INNER JOIN ( SELECT Col1 AS REG, MIN(Col2) AS MINDATE
FROM TBL1
GROUP BY Col1) AS T2
ON T1.Col1 = T2.REG and T1.Col2 = T2.MINDATE
The result correctly recognises for each unique registration the oldest date and sets 'Y' in col3.
I need to replicate this process in MYSQL but after much effort I can't find a way to do this?
If you are just looking for the same logic, but with MySQL's update join syntax, then try this:
UPDATE TBL1 T1
INNER JOIN
(
SELECT Col1 AS REG, MIN(Col2) AS MINDATE
FROM TBL1
GROUP BY Col1
) AS T2
ON T1.Col1 = T2.REG and T1.Col2 = T2.MINDATE
SET T1.Col3 = 'Y';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.