jeudi 24 novembre 2016

How to chunk properly a query with subquery in Laravel

My problem is that I need to get at any moment the duplicate entries on mel column from dest table.

I've tried so many things to translate this query in Laravel but couldn't find the way :

select id
from dest d,
     (select mel from dest where stt = 0 group by mel having count(mel) > 1) sr
where d.mel = sr.mel

I've tried to create a view

create DEFINER = user view dest_doubles

as select id from dest d, (select mel from dest where stt = 0 group by mel having count(mel) > 1) sr where d.mel = sr.mel

The select id from dest_doubles query lasts forever whereas

select id from dest d, (select mel from dest where stt = 0 group by
mel having count(mel) > 1) sr where d.mel = sr.mel

responds in 3 minutes

I've tried this in Laravel command :

\DB::select(\DB::raw('dest.id'))
                        ->from(\DB::raw('dest, (select mel from dest where stt = 0 group by mel having count(mel) > 1) as sr'))
                        ->whereRaw('dest.mel = sr.mel')
                        ->chunk(10000, function ($doubles) {

});

And could not execute :

[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'dest.id' at line 1

So I don't know how to a query that contains a subquery in Laravel

Thank you for helping



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire