Since it is a duplicate control, a possible solution would be a query like this:
SELECT
p.codigo,
GROUP_CONCAT(p.id SEPARATOR '|') ids,
COUNT(*) total
FROM
presupuestosa p
WHERE p.obra=18 AND p.empresa=1
GROUP BY p.codigo, p.obra, p.empresa;
This query will group by codigo, obra, empresa
and will throw in the column total
the number of records in each group. The column ids
achieved with GROUP_CONCAT
is a plus, in case you are interested to know for example what the id of each value repeated, will be separated by |
.
This will allow you simpler and easier to analyze results.
Proof of concept
VIEW DEMO IN REXTESTER
I did a test based on real data (I did not put all the records that you show, only a few for reasons of brevity) and I put a case in which there are three duplicates, to show that the query can have an interesting scope: know at once how many duplicates there are ...
CREATE TABLE IF NOT EXISTS presupuesto_20180729
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
codigo INT,
obra INT,
empresa INT
)ENGINE=INNODB;
INSERT INTO presupuesto_20180729 (codigo, obra, empresa) VALUES
(1,1,1),
(2,1,1),
(3,18,1),
(5,18,1),
(5,18,1),
(5,18,1),
(7,18,1),
(11,18,1),
(11,18,1)
;
SET sql_mode = 'ONLY_FULL_GROUP_BY';
SELECT
p.codigo,
GROUP_CONCAT(p.id SEPARATOR '|') ids,
COUNT(*) total
FROM
presupuesto_20180729 p
WHERE p.obra=18 AND p.empresa=1
GROUP BY p.codigo, p.obra, p.empresa;
Result:
codigo ids total
-------------------------
3 3 1
5 4|5|6 3
7 7 1
11 8|9 2