我的MySql函数继续给我语法错误消息:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in
your SQL syntax; check the manual that corresponds to your MySQL server version
for the right syntax to use near 'SELECT SUM(sell_rate) FROM (SELECT
COUNT(stock_id)*stock_rate sell_rate
FRO' at line 2 (SQL: select
SELECT SUM(sell_rate) FROM (SELECT COUNT(stock_id)*stock_rate sell_rate
FROM tbl_stock st
INNER JOIN tbl_product pdt ON pdt.product_id = st.produck_id
WHERE produck_name = 'product_id' lIMIT 6
GROUP BY stock_id)
from `tbl_stock`)
我已经检查了所有括号并且它们都已关闭我甚至在MySQL工作台上执行了它并且它很好但是在Laravel它给了我这个错误 这是我的功能
private function totalprice( $product_id)
{
$selltotal = DB::table('tbl_stock')
->select(DB::raw("
SELECT SUM(sell_rate) FROM (SELECT COUNT(stock_id)*stock_rate sell_rate
FROM tbl_stock st
INNER JOIN tbl_product pdt ON pdt.product_id = st.produck_id
WHERE produck_name = 'product_id' lIMIT 6
GROUP BY stock_id)
"))->get();
return $selltotal;
}
答案 0 :(得分:0)
尝试DB::selet()
:
$selltotal = DB::select("SELECT SUM(sell_rate) FROM (SELECT COUNT(stock_id)*stock_rate sell_rate
FROM tbl_stock st
INNER JOIN tbl_product pdt ON pdt.product_id = st.produck_id
WHERE produck_name = 'product_id' lIMIT 6)
GROUP BY stock_id");
答案 1 :(得分:-1)
不要使用SELECT,只需尝试DB ::语句。您还缺少应在LIMIT 6
$selltotal = DB::statement("SELECT SUM(sell_rate)
FROM
(SELECT COUNT(stock_id) * stock_rate sell_rate
FROM tbl_stock st
INNER JOIN tbl_product pdt ON pdt.product_id = st.produck_id
WHERE produck_name = 'product_id'
LIMIT 6)
GROUP BY stock_id");