Tech Kid Tech Kid - 3 months ago 7
MySQL Question

I want to update the duplicate table entries

I have a table of

fav_tweet_ids
. in which I catch the people's favorite
tweet_ids
. I see many people like the same tweet, and in column I see a lot of same
tweet_ids
. I want to select all the those rows and also I want to update all those rows.

My columns are

id
,
username
,
tweet_id
,
date
,
processing
,
status
,
favs_status
.

I want to update
status
=0 and
processing
=1 and
favs_status
= 0.

this is sample data:

INSERT INTO `sent_retweet_tweet_ids` (`id`, `username`, `activity_amount`, `tweet_id`, `user_id`, `date`, `processing`, `status`, `favs_status`) VALUES
(1, 'Funky_Kudi', '55', '763249955360411648', '1', '2016-08-10 08:51:29', 0, 1, '1'),
(2, 'sumimol12high', '55', '763249263375749120', '2', '2016-08-10 09:30:18', 0, 1, '1'),
(3, 'sumimol12high', '55', '763245287338946560', '2', '2016-08-10 09:43:38', 0, 1, '1'),
(4, 'gorgeousupneja', '55', '763249911668285440', '3', '2016-08-10 10:06:28', 0, 1, '1'),
(5, 'gorgeousupneja', '55', '763247421744934913', '3', '2016-08-10 10:19:58', 0, 1, '1'),
(6, 'gorgeousupneja', '55', '763243340410347520', '3', '2016-08-10 10:33:20', 0, 1, '1'),
(7, 'gorgeousupneja', '55', '762979902995898368', '3', '2016-08-10 10:46:42', 0, 1, '1'),
(8, 'sumanphogat123', '55', '763249181649674240', '4', '2016-08-10 11:00:08', 0, 1, '1'),
(9, 'sumanphogat123', '55', '763245117243330560', '4', '2016-08-10 11:13:28', 0, 1, '1'),
(10, 'Prakharnaldo', '55', '763317390981537792', '5', '2016-08-10 11:26:47', 0, 1, '1'),
(11, 'Prakharnaldo', '55', '763251799356649472', '5', '2016-08-10 11:40:14', 0, 1, '1'),
(12, 'Prakharnaldo', '55', '763250442377715712', '5', '2016-08-10 11:53:39', 0, 1, '1'),
(13, 'Prakharnaldo', '55', '763250231383248896', '5', '2016-08-10 12:07:39', 0, 1, '1'),
(14, 'Prakharnaldo', '55', '763248103868796928', '5', '2016-08-10 12:21:17', 0, 1, '1'),
(15, 'Prakharnaldo', '55', '763244138926055424', '5', '2016-08-10 12:34:44', 0, 1, '1'),
(16, 'Prakharnaldo', '55', '762994137981345793', '5', '2016-08-10 12:48:05', 0, 1, '1'),
(17, 'Prakharnaldo', '55', '762991537307291648', '5', '2016-08-10 13:01:27', 0, 1, '1'),
(18, 'Prakharnaldo', '55', '762991427672428544', '5', '2016-08-10 13:15:07', 0, 1, '1'),
(19, 'Prakharnaldo', '55', '762988076205420544', '5', '2016-08-10 13:28:35', 0, 1, '1'),
(20, 'Prakharnaldo', '55', '762986428691124224', '5', '2016-08-10 13:41:53', 0, 1, '1'),
(21, 'Prakharnaldo', '55', '762972461356810240', '5', '2016-08-10 13:55:17', 0, 1, '1'),
(22, 'Prakharnaldo', '55', '762969531958435840', '5', '2016-08-10 14:08:43', 0, 1, '1'),
(23, 'Prakharnaldo', '55', '762965164098334721', '5', '2016-08-10 14:22:11', 0, 1, '1'),
(24, 'Prakharnaldo', '55', '762958117017571328', '5', '2016-08-10 14:35:27', 0, 1, '1'),
(25, 'Prakharnaldo', '55', '762921670772346880', '5', '2016-08-10 14:50:37', 0, 1, '1'),
(26, 'Prakharnaldo', '55', '762921082479218689', '5', '2016-08-10 15:04:02', 0, 1, '1'),
(27, 'Prakharnaldo', '55', '762920922546184193', '5', '2016-08-10 15:17:28', 0, 1, '1'),
(28, 'Prakharnaldo', '55', '762920778568306688', '5', '2016-08-10 15:30:52', 0, 1, '1'),
(29, 'Prakharnaldo', '55', '762919750632558597', '5', '2016-08-10 15:44:10', 0, 1, '1'),
(30, 'SforSanjay_', '55', '763365762895482880', '6', '2016-08-10 15:57:33', 0, 1, '1'),
(31, 'SforSanjay_', '55', '763317390998319104', '6', '2016-08-10 16:11:05', 0, 1, '1'),
(32, 'SforSanjay_', '55', '763251799394390020', '6', '2016-08-10 16:24:24', 0, 1, '1'),
(33, 'SforSanjay_', '55', '763250442792939520', '6', '2016-08-10 16:37:42', 0, 1, '1'),
(34, 'SforSanjay_', '55', '763250231534161920', '6', '2016-08-10 16:51:03', 0, 1, '1'),
(35, 'SforSanjay_', '55', '763248962262499330', '6', '2016-08-10 17:04:18', 0, 1, '1'),
(36, 'SforSanjay_', '55', '763244747968417792', '6', '2016-08-10 17:17:49', 0, 1, '1'),
(37, 'SforSanjay_', '55', '762996187880304641', '6', '2016-08-10 17:31:14', 0, 1, '1'),
(38, 'SforSanjay_', '55', '762994507105263616', '6', '2016-08-10 17:44:32', 0, 1, '1'),
(39, 'SforSanjay_', '55', '762991537449865219', '6', '2016-08-10 17:57:48', 0, 1, '1'),
(40, 'SforSanjay_', '55', '762991427710128130', '6', '2016-08-10 18:12:46', 0, 1, '1'),
(41, 'SforSanjay_', '55', '762989346358124544', '6', '2016-08-10 18:26:12', 0, 1, '1'),
(42, 'SforSanjay_', '55', '762986641069789184', '6', '2016-08-10 18:39:27', 0, 1, '1'),
(43, 'SforSanjay_', '55', '762980261801828352', '6', '2016-08-10 18:53:26', 0, 1, '1'),
(44, 'Prakharnaldo', '55', '763365762828337152', '5', '2016-08-10 19:15:23', 0, 1, '1'),
(45, 'SforSanjay_', '55', '763365762895482880', '6', '2016-08-10 19:21:52', 0, 1, '1'),
(46, '_harshyadav', '50', '763245751619256321', '7', '2016-08-10 19:33:57', 0, 1, '1'),
(47, '_harshyadav', '50', '763241871355285504', '7', '2016-08-10 19:40:08', 0, 1, '1'),
(48, '_harshyadav', '50', '763245751619256321', '7', '2016-08-10 19:42:04', 0, 1, '1'),
(49, '_harshyadav', '50', '762979902832250880', '7', '2016-08-10 19:46:47', 0, 1, '1'),
(50, '_harshyadav', '50', '763241871355285504', '7', '2016-08-10 19:47:18', 0, 1, '1'),
(51, '_harshyadav', '50', '762979902832250880', '7', '2016-08-10 19:52:34', 0, 1, '1'),
(52, 'lessbeerplease', '50', '763247672706887681', '8', '2016-08-10 19:57:47', 0, 1, '1'),
(53, 'lessbeerplease', '50', '763243537307725824', '8', '2016-08-10 20:03:25', 0, 1, '1'),
(54, 'romanticgupta', '50', '763248893035442176', '9', '2016-08-10 20:08:43', 0, 1, '1'),
(55, 'romanticgupta', '50', '763244621354954753', '9', '2016-08-10 20:14:25', 0, 1, '1'),
(56, 'MUFCabhishek', '50', '763247987829141504', '10', '2016-08-10 20:19:46', 0, 1, '1'),
(57, 'MUFCabhishek', '50', '763243972357656576', '10', '2016-08-10 20:25:08', 0, 1, '1'),
(58, 'london_thumakda', '50', '763247527550484480', '11', '2016-08-10 20:30:22', 0, 1, '1'),
(59, 'london_thumakda', '50', '763243437407739909', '11', '2016-08-10 20:35:41', 0, 1, '1'),
(60, 'tanya_lazy', '50', '763249856316080128', '12', '2016-08-10 20:40:58', 0, 1, '1'),
(61, 'tanya_lazy', '50', '763245537055371264', '12', '2016-08-10 20:46:15', 0, 1, '1'),
(62, 'sjoshi324', '50', '763249112917635072', '13', '2016-08-10 20:51:54', 0, 1, '1'),
(63, 'sjoshi324', '50', '763244927346151424', '13', '2016-08-10 20:57:10', 0, 1, '1'),
(64, 'sjoshi324', '50', '763244856223404036', '13', '2016-08-10 21:02:22', 0, 1, '1'),
(65, 'ramvaish123', '50', '763248357439578112', '14', '2016-08-10 21:07:46', 0, 1, '1'),
(66, 'ramvaish123', '50', '763244310477303808', '14', '2016-08-10 21:13:09', 0, 1, '1'),
(67, 'divy4nshu', '50', '763365762731876352', '15', '2016-08-10 21:18:22', 0, 1, '1'),
(68, 'divy4nshu', '50', '763317390889267200', '15', '2016-08-10 21:23:39', 0, 1, '1'),
(69, 'divy4nshu', '50', '763251794940002304', '15', '2016-08-10 21:28:58', 0, 1, '1'),
(70, 'divy4nshu', '50', '763250438858690560', '15', '2016-08-10 21:34:16', 0, 1, '1'),
(71, 'divy4nshu', '50', '763250230250704896', '15', '2016-08-10 21:39:33', 0, 1, '1'),
(72, 'divy4nshu', '50', '763247066973962240', '15', '2016-08-10 21:44:45', 0, 1, '1'),
(73, 'divy4nshu', '50', '763243048851693568', '15', '2016-08-10 21:49:55', 0, 1, '1'),
(74, 'divy4nshu', '50', '762992996233031681', '15', '2016-08-10 21:55:13', 0, 1, '1'),
(75, 'divy4nshu', '50', '762991537256968192', '15', '2016-08-10 22:00:26', 0, 1, '1'),
(76, 'divy4nshu', '50', '762991427810889728', '15', '2016-08-10 22:05:40', 0, 1, '1'),
(77, 'divy4nshu', '50', '762986781977419777', '15', '2016-08-10 22:11:02', 0, 1, '1'),
(78, 'divy4nshu', '50', '762983290366156800', '15', '2016-08-10 22:16:24', 0, 1, '1'),
(79, 'divy4nshu', '50', '762975732922404865', '15', '2016-08-10 22:21:40', 0, 1, '1'),
(80, 'divy4nshu', '50', '762969531773890562', '15', '2016-08-10 22:26:52', 0, 1, '1'),
(81, 'divy4nshu', '50', '762960652876910592', '15', '2016-08-10 22:32:32', 0, 1, '1'),
(82, 'divy4nshu', '50', '762953081378131969', '15', '2016-08-10 22:37:44', 0, 1, '1'),
(83, 'divy4nshu', '50', '762948815707975680', '15', '2016-08-10 22:42:56', 0, 1, '1'),
(84, 'divy4nshu', '50', '762921669740498944', '15', '2016-08-10 22:48:09', 0, 1, '1'),
(85, 'divy4nshu', '50', '762921081539747840', '15', '2016-08-10 22:53:27', 0, 1, '1'),
(86, 'divy4nshu', '50', '762920921585758209', '15', '2016-08-10 22:58:40', 0, 1, '1'),

Answer
;WITH cte AS (
    SELECT 
       *
       ,ROW_NUMBER() OVER (PARTITION BY tweet_id, user_id ORDER BY id) as RowNum
    FROM
       fav_tweet_ids
WHERE
   fav_status = 1
)

UPDATE cte 
    SET processing = 1
       ,status = 0
       ,fav_status = 0
WHERE
    RowNum > 1

There are a few ways but this is one using an updatable common table expression which makes it really simple. The other ways is to first find all of the duplicate tweet_ids and then update with a join back to that.

MySQL version to update all but 1 row as desired. If you want all rows of user/tweet combination there are easier methods this is more than likely what you want to keep. This assumes you can use mysql variables:

Edited to update based on tweet_id only not users

UPDATE sent_retweet_tweet_ids u
  INNER JOIN 
  (
    SELECT
      id
      ,@rownum:=IF((@prevtweet = t.tweet_id), @rownum + 1, 1) as RowNum
      ,@prevtweet:= t.tweet_id
    FROM
       sent_retweet_tweet_ids t
       CROSS JOIN (SELECT @rownum:=0, @prevtweet:=0) var
    WHERE
        t.fav_status = 1
    ORDER BY
        t.date
    ) r
    ON u.id = r.id
    AND r.RowNum > 1
SET
  u.processing = 1
  ,u.status = 0
  ,u.fav_status = 0

SQL Fiddle examples:

Update only when user likes the tweet more than once: http://sqlfiddle.com/#!9/42fb74/1

Update 9 or 10 users when more than 1 like the same tweet: http://sqlfiddle.com/#!9/31c9e

Comments