lundi 22 avril 2019

Laravel: Query works directly on MySQL but not on DB

I have the following query:

SELECT cn.id, cn.title, cn.type, cn.status FROM (
    SELECT v.concert_id as concert_id, SUM(v.position_votes + v.support_votes) as votes
    FROM (
        SELECT q.concert_id as concert_id, q.position_id as position_id, q.position_votes as position_votes, SUM(q.votes_up + q.votes_down) as support_votes
        FROM ( 
            SELECT qcs.id AS concert_id, p.id AS position_id, p.votes AS position_votes, IF(s.votes_in_favor <=> null, 0, s.votes_in_favor) AS votes_up, IF(s.votes_not_in_favor <=> null, 0, s.votes_not_in_favor) AS votes_down     
            FROM (
                SELECT concert_id AS id 
                FROM positions p 
                WHERE p.content LIKE '%%' 
                GROUP BY concert_id
            ) 
            AS qcs 
            JOIN positions p
            ON p.concert_id = qcs.id
            LEFT JOIN supports s
            ON s.position_id = p.id
        ) AS q
        GROUP BY q.position_id
    ) as v
    GROUP BY v.concert_id
) as r
JOIN concerts cn on cn.id = r.concert_id
ORDER BY r.votes DESC, cn.created_at DESC

When I made that query directly into MySQL I get the desired results. But when I put that query using DB, for example:

$query = "...alll_the_previous_query";
$result = DB::select(DB::raw($query));

I got the following error:

local.ERROR: SQLSTATE[42000]: Syntax error or access violation: 1055 'q.concert_id' isn't in GROUP BY ...the rest of the query

I know that a way to avoid this is to change the database configuration in Laravel and change the strict to false. But that is not an option.

What is wrong with my query when I passed it to Laravel?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire