Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 26, 2023 03:06
Show Gist options
  • Save code-boxx/f8bb567b8d58d6461f1136250d162010 to your computer and use it in GitHub Desktop.
Save code-boxx/f8bb567b8d58d6461f1136250d162010 to your computer and use it in GitHub Desktop.
MYSQL get & count duplicate entries

GET & COUNT DUPLICATE RECORDS IN MYSQL

https://code-boxx.com/get-count-duplicate-records-mysql/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

-- (A) FOOD TABLE
CREATE TABLE `food` (
`id` bigint(20) NOT NULL,
`name` varchar(255) NOT NULL,
`qty` bigint(20) NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `food`
ADD PRIMARY KEY (`id`);
ALTER TABLE `food`
MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT;
-- (B) DUMMY ENTRIES
INSERT INTO `food` (`id`, `name`, `qty`) VALUES
(1, 'Candy', 57),
(2, 'Pasta', 123),
(3, 'Apple', 24),
(4, 'Orange', 67),
(5, 'Pasta', 8),
(6, 'Melon', 64),
(7, 'Apple', 31),
(8, 'Pasta', 12),
(9, 'Rice', 87),
(10, 'Candy', 247);
SELECT `name`, COUNT(*) AS `rows`
FROM `food`
GROUP BY `name`
HAVING `rows` > 1
SELECT *
FROM `food`
WHERE `name` IN (
SELECT `name`
FROM `food`
GROUP BY `name`
HAVING COUNT(*) > 1
)
SELECT `name`, SUM(`qty`) AS `total`
FROM `food`
GROUP BY `name`
-- (A) CREATE NEW "STAGING" TABLE
CREATE TABLE `food-stage` (
`id` bigint(20) NOT NULL,
`name` varchar(255) NOT NULL,
`qty` bigint(20) NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `food-stage`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `name` (`name`);
ALTER TABLE `food-stage`
MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT;
<?php
// (A) CONNECT TO DATABASE
$pdo = new PDO(
"mysql:host=localhost;dbname=test;charset=utf8",
"USER", "PASSWORD", [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
// (B) GET SUM OF FOODS
$stmtA = $pdo->prepare("SELECT `name`, SUM(`qty`) AS `total` FROM `food` GROUP BY `name`");
$stmtA->execute();
while ($row = $stmtA->fetch()) {
$stmtB = $pdo->prepare("INSERT INTO `food-stage` (`name`, `qty`) VALUES (?,?)");
echo $row["name"] ." - ". $row["total"] . " - ";
echo $stmtB->execute([$row["name"], $row["total"]]) ? "OK" : "ERROR" ;
echo "<br>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment