简体   繁体   中英

update a column in sql using two conditions

I have a table in mysql in which i want to update the records in a column called status to 'Duplicate'. I want to mark a record 'duplicate' on the basis of 2 conditions.

  1. The records have duplicate customer id.

  2. Those records which don't have the recent modified date will be marked duplicate.

附上数据的图像以供参考

I have tried the below code but it gives me an error:

UPDATE test_sql_duplicate
SET status = 'Duplicate'
WHERE test_sql_duplicate.modi_date NOT IN (
    SELECT *, max(modi_date) 
    FROM test_sql_duplicate
    GROUP BY cust_id 
    HAVING COUNT(cust_id > 1)

To string together conditions, use AND (can also use OR ). Example:

`SELECT *
FROM products
WHERE department = 'beverages'
AND price < 15
AND distributer = 'prairie farms';`

I suspect we may be wanting a query something like this:

 UPDATE TEST_SQL_DUPLICATE t
   JOIN ( 
          SELECT n.cust_id
               , MAX(n.modi_date) AS max_modi_date
            FROM TEST_SQL_DUPLICATE n
           GROUP
              BY n.cust_id
          HAVING COUNT(n.cust_id) > 1
      ) d
    ON d.cust_id       = t.cust_id
   AND d.max_modi_date > t.modi_date
   SET t.status = 'Duplicate'

Given sample data:

 _row  cust_id    modi_date
       -------   ----------
    1      444   2019-10-28
    2      444   2019-10-28
    3      444   2019-10-29
    4      444   2019-10-30
    5      444   2019-10-30

the query in this answer would flag rows 1 thru 3, set status column to to 'Duplicate' . Rows 4 and 5 would not be marked, because they both have the same (maximum) modi_date .

We would also achieve the same result if we omitted the HAVING clause from the inline view query.

You could use a LEFT JOIN antipattern to identify the records to update. Basically we use a subquery to identify the latest record for each customer, then we use it to exclude the corresponding from the update query:

UPDATE test_sql_duplicate t
LEFT JOIN (
    SELECT cust_id, MAX(modi_date) modi_date FROM test_sql_duplicate GROUP BY cust_id
) m ON m.cust_id = t.cust_id and m.modi_date = t.modi_date
SET t.status = 'Duplicate'
WHERE m.cust_id IS NULL

Here is a quick and dirty way:

    UPDATE test_sql_duplicate SET status = 'Duplicate' 
    WHERE cust_id IN (
       SELECT t.id FROM (
          SELECT 
             modi_date date,
             cust_id id, 
             COUNT(*) OVER(PARTITION BY cust_id) cnt,
             MAX(modi_date) OVER() maxDate 
             FROM test_sql_duplicate
          ) t 
       WHERE t.date < maxDate OR t.cnt > 1);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM