logo
down
shadow

Rails - Duplicate record with update action


Rails - Duplicate record with update action

By : user2954362
Date : November 22 2020, 01:01 AM
like below fixes the issue Your form_for is always pointing to create route, you can check your routes running rake routes in you app root.
Instead, replace first line in _form partial:
code :
<%= form_for :post, url: posts_path do |post| %>
<%= form_for @post do |post| %>


Share : facebook icon twitter icon
Find duplicate mysql record and update first duplicate with last duplicate value

Find duplicate mysql record and update first duplicate with last duplicate value


By : user2834047
Date : March 29 2020, 07:55 AM
wish of those help For the UPDATE query, we need to get the min and max ID's for each tag, only if there are duplicate rows:
code :
UPDATE table t
  JOIN (
    SELECT MinID, b.Name LatestName
    FROM table b
    JOIN (
      SELECT MIN(ID) MinID, MAX(ID) MaxID
      FROM table
      GROUP BY tag
      HAVING COUNT(*) > 1
    ) g ON b.ID = g.MaxID
  ) rs ON t.ID = rs.MinID
SET t.Name = LatestName;
DELETE t.*
FROM table t
  LEFT JOIN (
    SELECT MIN(ID) MinID
    FROM table
    GROUP BY tag
  ) g ON t.ID = g.MinID
WHERE g.MinID IS NULL;
update/insert the record - ON DUPLICATE KEY UPDATE- not update the record

update/insert the record - ON DUPLICATE KEY UPDATE- not update the record


By : Max Test Ultra
Date : March 29 2020, 07:55 AM
this will help I'm trying to make the statement that inserts or updates the record depending on its presence in the table. If the record in the "drug_name_pl" field is present, it will increase its "vote_sum" value by 1. The problem is that the statement creates evey time the a record while trying to update already existing field. , you need to add unique key on drug_name_pl
something like:
code :
CREATE UNIQUE INDEX drug_name_pl_unique ON table_name (drug_name_pl)
SQL statement to update duplicate record keeping 1 record unchanged

SQL statement to update duplicate record keeping 1 record unchanged


By : Dmytro Smirnov
Date : March 29 2020, 07:55 AM
around this issue Perhaps you may give it a try with a variable:
SQLFIDDLE DEMO
code :
set @sku:='';

select a.rid, a.id, 
@sku:= (case when @sku<>a.sku
        then a.sku else 'updated'
        end) as skus, @sku:=a.sku, a.name
from skus a
;

| RID | ID |    SKUS | @SKU:=A.SKU |           NAME |
-----------------------------------------------------
|   1 |  3 |  rs-123 |      rs-123 |   test product |
|   2 |  3 | updated |      rs-123 |   test product |
|   3 |  4 |  rs-125 |      rs-125 | test product 2 |
|   4 |  4 | updated |      rs-125 | test product 2 |
|   5 |  4 | updated |      rs-125 | test product 2 |
|   6 |  6 |  rs-126 |      rs-126 | test product 3 |
set @sku:='';

UPDATE
  skus a
  join 
  (select a.rid, a.id, 
@sku:= (case when @sku<>a.sku
        then a.sku else 'updated'
        end) as skus, @sku:=a.sku, a.name
from skus a) b
  on a.rid = b.rid
SET
  a.sku = 'up_again'
WHERE
  b.skus = 'updated'
;

| RID | ID |      SKU |           NAME |
----------------------------------------
|   1 |  3 |   rs-123 |   test product |
|   2 |  3 | up_again |   test product |
|   3 |  4 |   rs-125 | test product 2 |
|   4 |  4 | up_again | test product 2 |
|   5 |  4 | up_again | test product 2 |
|   6 |  6 |   rs-126 | test product 3 |
update skus a
join skus b
on a.rid = b.rid +1
set a.sku = 'updated'
where a.sku = b.sku
;


| RID | ID |     SKU |           NAME |
---------------------------------------
|   1 |  3 |  rs-123 |   test product |
|   2 |  3 | updated |   test product |
|   3 |  4 |  rs-125 | test product 2 |
|   4 |  4 | updated | test product 2 |
|   5 |  4 | updated | test product 2 |
|   6 |  6 |  rs-126 | test product 3 |
update skus a
join skus b
on a.rid = b.rid +1
set a.sku = 'updated', a.name = 'new_name'
where a.sku = b.sku
;

| RID | ID |     SKU |           NAME |
---------------------------------------
|   1 |  3 |  rs-123 |   test product |
|   2 |  3 | updated |       new_name |
|   3 |  4 |  rs-125 | test product 2 |
|   4 |  4 | updated |       new_name |
|   5 |  4 | updated |       new_name |
|   6 |  6 |  rs-126 | test product 3 |
update a 
set a.sku = 'updated', a.name = 'new_name'
from skus a
join (Select rid, id, row_number() over (
  partition by id order by rid asc) rank
from skus) b
on a.rid = b.rid
where b.rank > 1
;

| RID | ID |     SKU |           NAME |
---------------------------------------
|   1 |  3 |  rs-123 |   test product |
|   2 |  3 | updated |       new_name |
|   3 |  4 |  rs-125 | test product 2 |
|   4 |  4 | updated |       new_name |
|   5 |  4 | updated |       new_name |
|   6 |  6 |  rs-126 | test product 3 |
mysql update query to keep same value for duplicate record and increment value for new record

mysql update query to keep same value for duplicate record and increment value for new record


By : manoj thapa
Date : March 29 2020, 07:55 AM
Hope that helps If you were using a more versatile database such as SQL Server, we could just assign a rank inside a CTE of your table, and then directly update that CTE. But you are using MySQL, so the problem becomes harder.
One approach is to generate the package_id values off the following table:
code :
id package_id package_code
1             001234
4             001200
5             001240
7             001275
SELECT package_code,
    (SELECT COUNT(*) FROM table t2 WHERE t2.id <= t1.id) package_id
FROM table t1;
UPDATE yourTable a
INNER JOIN
(
    SELECT
        package_code,
        (SELECT COUNT(*) FROM (SELECT package_code, MIN(id) AS id
                               FROM yourTable
                               GROUP BY package_code) t2
         WHERE t2.id <= t1.id) AS rn
    FROM
    (
        SELECT package_code, MIN(id) AS id
        FROM yourTable
        GROUP BY package_code
    ) t1
) b
    ON a.package_code = b.package_code
SET package_id = b.rn;
Rails update a record that has duplicate ids in db

Rails update a record that has duplicate ids in db


By : user3183098
Date : March 29 2020, 07:55 AM
Hope that helps I have a db with records that might have other records with the same id (yep, it really do be like that). , I think this is what you are looking for,
code :
Member.where(id: record.id, crew_id: record.crew_id).update_all(values)
Related Posts Related Posts :
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org