简体   繁体   English

Laravel将多个SQL选择为AS

[英]Laravel multiple sql select to AS

I'm trying to select multiple database columns and to merge them to an AS value statement like SELECT sum1 as vsd FROM tablename WHERE year = 2017 我试图选择多个数据库列并将其合并到AS值语句中,例如SELECT sum1 as vsd FROM tablename WHERE year = 2017

I have multiple columns I need to output as one value. 我有多个列需要作为一个值输出。 So I imagine the above code looking something like this: 所以我想上面的代码看起来像这样:

SELECT (sum1, sum2) as vsd FROM tablename WHERE year = 2017

My current live-code query looks like this: 我当前的实时代码查询如下所示:

$query = DB::table('exports')->select('id', 'year', 'week', 'month', 'staff_a, staff_b as vsd')->having('year','=',$year)->where('week', '<=', $weeknr)->orderBy('id', 'desc')->take(14)->get()->reverse();

The result is: 结果是:

Column not found: 1054 Unknown column 'staff_a, staff_b' in 'field list' 找不到列:1054“字段列表”中的未知列“ staff_a,staff_b”

I tried to combine them (columns) to an array ( array('staff_a', 'staff_b') ), had some weird foreach-loops and whatnot trying to make it work another way but all that threw yet another error message. 我试图将它们(列)组合成一个数组( array('staff_a', 'staff_b') ),有一些怪异的foreach循环,但并没有尝试使其以另一种方式起作用,但这一切都引发了另一个错误消息。

Is there any way to "merge" multiple selects to one? 有什么办法可以将多个选择“合并”到一个?

You have to use RAW syntax: 您必须使用RAW语法:

->select(DB::raw('staff_a + staff_b as vsd'))

Ref: https://laravel.com/docs/5.5/queries#raw-expressions 参考: https : //laravel.com/docs/5.5/queries#raw-expressions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM