Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gendylopez's avatar

MySQL Query Prepared Statement to Laravel

I spent hours making this query right using phpMyAdmin and when I was already going to transfer it on my laravel project, it says there is an error, and I really don't understand what the error is.

   SELECT GROUP_CONCAT(DISTINCT CONCAT(
       'SUM( CASE WHEN rate_sub_type = ''',rate_sub_type,''' THEN rate END ) AS rate_sub_type'
       )
       ) INTO @sql
   FROM rate_sub_types;

   SET @query = CONCAT('SELECT rate_type, name, ', @sql, '
   FROM rate_types a, rate_sub_types b, rates c 
   where a.id=b.rate_type_id and b.id=c.rate_sub_type_id and c.amenity_id is null GROUP BY rate_type, name
   UNION
   SELECT rate_type, d.name, ', @sql, '
   FROM rate_types a, rate_sub_types b, rates c, amenities d
   where a.id=b.rate_type_id and b.id=c.rate_sub_type_id and d.id=c.amenity_id
   GROUP BY rate_type, d.name');

   PREPARE stmt FROM @query;
   EXECUTE stmt;

This is how i put it on laravel...

http://imgur.com/0hUz4P9

These are the errors...

http://imgur.com/DhHwb9R http://imgur.com/lhLrK2m

Anyone who could help me please? Thank you so much

0 likes
3 replies
ndberg's avatar

Try it with DB::unprepared()

1 like

Please or to participate in this conversation.