Laravel 4 如何在Laravel4中将MySQL查询转换为DB builder?
如何将thsis MYSQL查询转换为Laravel sql查询Laravel 4 如何在Laravel4中将MySQL查询转换为DB builder?,laravel-4,Laravel 4,如何将thsis 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,
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()…
还有,例如:$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语句。这里是如何使用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语句。我建议您使用DB::raw和DB::select这是无效的MySQL语法。看起来更像SQL Server。如何在DB::RAW和DB::SELECT中编写此代码。。方法我建议您使用DB::RAW和DB::SELECT,这是无效的MySQL语法。看起来更像SQL Server。如何在DB::RAW和DB::SELECT中编写此代码。。方法