Forum » Programiranje » MySql
MySql
BRBR ::
DELETE FROM detail WHERE id IN ( SELECT MAX(id) AS id FROM master GROUP BY term HAVING COUNT(term) > 1 )
Zakaj tale melje 100 let, čakam 20 min pa ni rezultata. Inner da rezultat v 6 sec, +/-20 recordov
outer ima +/- 300.000 recordov
- spremenil: BRBR ()
kuall ::
The EXISTS clause is faster than IN when the subquery results are very large. The IN clause is faster than EXISTS when the subquery results are very small.
jaz nikoli ne uporabljam in, vedno exists, pa programiram v sql vsak dan. in je neuporaben, exists je hitrejša alternativa. to vse od takrat, ko sem prebral, da je exists hitrejši in tudi intuicija ti lahko govori, da je exists hitrejši. in vsakič zbere vse rezultate, ko izbriše eno vrstico. exists pa neha takoj, ko najde prvo.
jaz nikoli ne uporabljam in, vedno exists, pa programiram v sql vsak dan. in je neuporaben, exists je hitrejša alternativa. to vse od takrat, ko sem prebral, da je exists hitrejši in tudi intuicija ti lahko govori, da je exists hitrejši. in vsakič zbere vse rezultate, ko izbriše eno vrstico. exists pa neha takoj, ko najde prvo.
BRBR ::
OK, dela bliskovito, ....
In kako to deluje z delete, će v delete part ni kolumna, ki je v inner part
Tale zbriše kar komplet vse v tabeli. V čem je fora ?
Inner part da ven 20 id-jev.
In kako to deluje z delete, će v delete part ni kolumna, ki je v inner part
DELETE FROM detail WHERE EXISTS ( SELECT MAX(id) AS id FROM master GROUP BY term HAVING COUNT(term) > 1 )
Tale zbriše kar komplet vse v tabeli. V čem je fora ?
Inner part da ven 20 id-jev.
Zgodovina sprememb…
- spremenil: BRBR ()
kuall ::
Nekam v inner sql moraš vštulit where detail.id je master.id. čeprav bi jaz raje naredu cte iz inner sql in join nanj.nisem zihr ali je exists tu ok rešitev.
klemenSLO ::
KAJ TAKEGA?
DELETE FROM detail D
JOIN
--WHERE EXISTS
(
SELECT MAX(id) AS id
FROM master
GROUP BY term
HAVING COUNT(term) > 1
) X ON X.ID =D.ID
DELETE FROM detail D
JOIN
--WHERE EXISTS
(
SELECT MAX(id) AS id
FROM master
GROUP BY term
HAVING COUNT(term) > 1
) X ON X.ID =D.ID
Life is not measured in minutes, but in MOMENTS...
kuall ::
težko kodirat brez tabel ampak kaj takega bo najbrž potrebno:
;WITH CTE AS ( SELECT MAX(id) AS id FROM master m GROUP BY m.term HAVING COUNT(term) > 1 ) SELECT * -- DELETE d FROM detail d WHERE EXISTS (SELECT 1 FROM CTE c WHERE d.id = c.id)
Zgodovina sprememb…
- spremenilo: kuall ()
DamijanD ::
Ne vem, če mysql podpira to notacijo ampak mogoče bi lahko tako poskusil:
with cte as (SELECT MAX(id) AS id
FROM master m
GROUP BY m.term
HAVING COUNT(term) > 1
)
delete e
from master e
inner join cte on cte.id = e.id
na ms sql to deluje
exists tukaj ni ok pristop imho, ker ti hočeš izbrisati največji ID od "duplikatov"
with cte as (SELECT MAX(id) AS id
FROM master m
GROUP BY m.term
HAVING COUNT(term) > 1
)
delete e
from master e
inner join cte on cte.id = e.id
na ms sql to deluje
exists tukaj ni ok pristop imho, ker ti hočeš izbrisati največji ID od "duplikatov"
DamijanD ::
my bad:
with cte as (SELECT MAX(id) AS id
FROM master m
GROUP BY m.term
HAVING COUNT(term) > 1
)
delete e
from detail e
inner join cte on cte.id = e.id
with cte as (SELECT MAX(id) AS id
FROM master m
GROUP BY m.term
HAVING COUNT(term) > 1
)
delete e
from detail e
inner join cte on cte.id = e.id
kuall ::
jp join bo kr pravi kle, exists/in ni potreben tu.
sem malo pobrkljal oba možna načina:
NAČIN 1:
NAČIN 2:
sem malo pobrkljal oba možna načina:
NAČIN 1:
;with cte as ( select key from detail oi where oi.product like '%z%' group by key having COUNT (oi.product) > 1 ) select * from master o join cte c on c.key = o.key
NAČIN 2:
select * from master o where (select COUNT (1) from detail oi2 where oi2.key = o.key and oi2.product like '%z%') > 1
Vredno ogleda ...
Tema | Ogledi | Zadnje sporočilo | |
---|---|---|---|
Tema | Ogledi | Zadnje sporočilo | |
» | SQLOddelek: Programiranje | 1115 (768) | no comment |
» | Učenje programiranja PHPOddelek: Programiranje | 1497 (1038) | Spura |
» | sql, negacija ..Oddelek: Programiranje | 722 (617) | BRBR |
» | Normalizirana struktura - queryOddelek: Programiranje | 1734 (1354) | frudi |
» | MYSQL vprašanjeOddelek: Programiranje | 1809 (1424) | MrBrdo |