php - Multiple line query in laravel

760

I am trying to execute multiple line query. I'm quite new to laravel. Here is my example query.

SET @sql = NULL;

SELECT

GROUP_CONCAT(DISTINCT

    CONCAT(
      'MAX(IF(`order` = ', `order`, ',data,NULL)) AS data', `order`)
  ) INTO @sql

FROM TableName;


SET @sql = CONCAT('SELECT  ID, ', @sql, ' 

                  FROM    TableName

                  GROUP   BY ID');

PREPARE stmt FROM @sql;

EXECUTE stmt;
462

Answer

Solution:

I was trying to dynamically converts columns into row, that's why I was using multiple queries. But then I manually write single query since there were only 4 unique entries in my order table, so it become more simple. Thanks for help..

People are also looking for solutions to the problem: php - MySQL: order by sum

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.