如何将 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编写代码?
以下是如何使用
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语句。