Laravel查询语句

时间:2017-09-19 09:49:11

标签: mysql laravel

这是一个Query,它通过声明变量然后准备语句并执行查询,将行中的数据转换为列中的数据。

现在我的问题是如何将此查询转换为laravel查询格式,请帮助我。

SET @sql = NULL, @sql1 = NULL, @sql2 = NULL;

SELECT GROUP_CONCAT(
           CONCAT('WHEN ''',  column_name, ''' THEN `', column_name, '`')
       SEPARATOR ' ')
  INTO @sql1
  FROM INFORMATION_SCHEMA.COLUMNS
 WHERE table_schema = SCHEMA()
   AND table_name = '7'
   AND column_name LIKE '7%'
 GROUP BY table_name;

SELECT GROUP_CONCAT(
           CONCAT('SELECT ''',  column_name, ''' objectid' )
       SEPARATOR ' UNION ALL ')
  INTO @sql2
  FROM INFORMATION_SCHEMA.COLUMNS
 WHERE table_schema = SCHEMA()
   AND table_name = '7'
   AND column_name LIKE '7%'
 GROUP BY table_name;

SET @sql = CONCAT(
'SELECT m.ObjectID, m.ObjectLabel, q.Value
  FROM modules_forms_objects m LEFT JOIN
(
  SELECT objectid,
         CASE objectid ', @sql1, ' END value 
    FROM `7` t CROSS JOIN
  (', @sql2, '
  ) c
) q`enter code here`
    ON m.objectid = q.objectid');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

1 个答案:

答案 0 :(得分:0)

您无法使用原始查询的任何原因?

DB::select(' { Your Code Here} ');

DB::raw(' {Your Code Here} ');