dqm4977 2014-12-29 07:26
浏览 973

Laravel:使用pluck从数据库中获取单个值

Hi everyone I'm using Laravel and I'm trying to get a single value from the database using pluck, the issue is the query must be written in the Laravel way not the normal sql.so how to write this sql query in Laravel ? I want to be able use AS to change the column name and SUM.all I want actually is to get the value from total_quantity column to do that I have to use pluck. any idea how to write that query ?

Thanks in advance.

$PhysicalStock=DB::select("select sum(quantity) as `total_quantity` from InOutProducts where productID=$productID and InAndOut=1;")->pluck('total_quantity');
  • 写回答

1条回答 默认 最新

  • doulu3399 2014-12-29 07:50
    关注

    Read more about Larvel DB and Eloquent.

    DB::table('InOutProducts')->selectRaw('SUM(quantity) as total_quantity')->where('productId', '=', $productID)->where('InAndOut', '=', 1)->pluck('total_quantity');
    
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法