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.