#'column' isn't in GROUP BY except that it is

9 messages · Page 1 of 1 (latest)

scenic drum
#

Hey all,
I'm trying to run the following SQL query to generate some summary data:

WITH ranged_tickets AS (
    SELECT tkt.order_id, tt.price AS price, ord.price AS order_price, ord.service_costs AS service_costs
    FROM `tickets` tkt
    JOIN `ticket_types` tt ON tt.id = tkt.ticket_type_id
    JOIN `orders` ord ON ord.id = tkt.order_

    WHERE ord.event_id IN (SELECT id FROM `events` WHERE organisation_id = ?) AND tkt.created_at BETWEEN ? AND ?
)

SELECT tkt.service_costs,
    (SELECT COUNT(*) FROM `ranged_tickets` tkt WHERE NOT tkt.price = 0) AS paid_tickets_sold,
    (SELECT COUNT(*) FROM `ranged_tickets` tkt WHERE tkt.price = 0) AS free_tickets_sold,
    IFNULL(SUM(tkt.price), 0) AS total_paid,
    IFNULL(tkt.service_costs * (
        SELECT SUM(
CASE WHEN tkt.service_costs = tkt_inner.service_costs AND tkt_inner.price > 0 THEN 1 END
        ) FROM `ranged_tickets` tkt_inner
    ), 0) AS total_service_costs
FROM `ranged_tickets` tkt
GROUP BY tkt.service_costs

Running this in PhpMyAdmin returns the correct data in the correct format. However, running the same code using DB::select("QUERY GOES HERE") runs into an error: Syntax error or access violation: 1055 'tkt.service_costs' isn't in GROUP BY. By trial and error, I found out that it's the CASE WHEN tkt.service_costs = (...) that causes the error; simply replacing it with a hardcoded integer solves the issue. I also tried using a WHERE instead of CASE, but to no avail.

I did some Google searches and found out that some people suggested turning off strict mode for the MySQL database in the Laravel config/database.php. However, this seems like a drastic resource and I'd strongly prefer keeping strict mode on. In that same post, someone suggest doing a full GROUP BY, but the column that is said to be missing is actually already present in the GROUP BY clause.

#

(I ran out of characters so I'm continuing here)

I'm out of ideas and I don't know what my next step should be, because I'm clueless as to why the error pops up. Any help is greatly appreciated!

cloud sluice
#

Does just running a select * on the with clause table work?

$sql = <<<SQL
WITH ranged_tickets AS (
    SELECT tkt.order_id, tt.price AS price, ord.price AS order_price, ord.service_costs AS service_costs
    FROM `tickets` tkt
    JOIN `ticket_types` tt ON tt.id = tkt.ticket_type_id
    JOIN `orders` ord ON ord.id = tkt.order_

    WHERE ord.event_id IN (SELECT id FROM `events` WHERE organisation_id = ?) AND tkt.created_at BETWEEN ? AND ?
)
SELECT *
FROM ranged_tickets
SQL;
DB::select($sql);
scenic drum
#

Yes, it's not throwing an error when I'm doing that

#

It also works with SELECT * FROM ranged_tickets tkt GROUP BY tkt.service_costs

cloud sluice
#

And if you add the columns you're selecting one by one?

#

I see you're selecting two columns

#

Does one of them break the query or do both of them make it fail?

scenic drum
#

It only fails on the line with CASE WHEN tkt.service_costs, because when I change only that tkt.service_costs to a hardcoded value, the program works as expected