如何在 Laravel 4 中将 MySQL 查询转换为 DB 构建器?

问题描述 投票:0回答:1

如何将 MYSQL 查询转换为 Laravel SQL 查询?

SELECT *
FROM (
    SELECT 
        year(invoiceDate) as [year],left(datename(month,invoicedate),3)as [month], 
        InvoiceAmount as Amount 
    FROM Invoice
) as s
PIVOT
(
    SUM(Amount)
    FOR [month] IN (jan, feb, mar, apr, 
    may, jun, jul, aug, sep, oct, nov, dec)
)AS pivot

请任何人使用这些方法将上述代码写入 Laravel DB Builder 格式。

例如:

$pivot=DB::table('Invoice')->where()...

还有,

eg: $pivote=Invoice::where()->..

如何为pivot编写代码?

php laravel-4
1个回答
0
投票

以下是如何使用

DB:raw
DB::select

实现代码
$results =
    DB::select(
        DB::raw("
          SELECT *
            FROM (
                SELECT 
                    year(invoiceDate) as [year],left(datename(month,invoicedate),3)as [month], 
                    InvoiceAmount as Amount 
                FROM Invoice
            ) as s
            PIVOT
            (
                SUM(Amount)
                FOR [month] IN (jan, feb, mar, apr, 
                may, jun, jul, aug, sep, oct, nov, dec)
            )AS pivot
        ")
    );

注意:这是为了展示/演示如何根据您在问题中的要求将SQL语句放入

DB::raw
中,但这并不意味着我必须检查您的SQL语句。

© www.soinside.com 2019 - 2024. All rights reserved.