hello every one please i need help how would I go about this select using Laravel's query builder? I am aware I can use a raw sql query but I would like to avoid that. Any help would be appreciated, thanks in advance!
SELECT SUM(COST) FROM (
SELECT COUNT(STOCK_ID)*PRICE COST
FROM STOCK ST INNER JOIN PRODUCT PDT ON PDT.PROD_ID=ST.PROD_ID
WHERE PROD_NAME='PENCIL' LIMIT 6 GROUP BY STOCK_ID) TB
The reason why i avoid the raw sql is because when i try it like this
private function totalprice( $product_id)
{
$selltotal =
DB::raw("SELECT SUM(sell_rate)
FROM
(SELECT COUNT(stock_id) * stock_rate
FROM tbl_stock AS st
INNER JOIN tbl_product AS pdt ON pdt.product_id = st.produck_id
WHERE produck_name = 'product_id'
LIMIT 6)
GROUP BY stock_id");
return $selltotal;
}
This is the error i get
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 ' at line 1 (SQL: insert into `tbl_sell` (`category_id`, `brand_id`, `product_id`, `buyer_id`, `sell_quantity`, `sell_total_price`, `sell_date`, `added_by`, `entry_time`) values (3, 5, 6, 3, 2, SELECT SUM(sell_rate)
FROM
(SELECT COUNT(stock_id) * stock_rate
FROM tbl_stock AS st
INNER JOIN tbl_product AS pdt ON pdt.product_id = st.produck_id
WHERE produck_name = 'product_id'
LIMIT 6)
GROUP BY stock_id, 2017-08-11, 0, 2017-08-11 07:34:11))
please i will be very greatfull if i get a solution to my problem. Thanks in advance
hello i succeded in doing it myself. It may help some one someday
private function totalprice( $product_id)
{
$selltotal = DB::table('tbl_stock')
->select(array(DB::raw('count(stock_id)*stock_rate')))
->where('tbl_stock.product_id', $product_id)
->join('tbl_product','tbl_stock.product_id','=', 'tbl_product.product_id')
->groupby('stock_id')
->sum('stock_rate');
return $selltotal;
}
Related
I am converting Oracle SQL into Laravel. The problem is how can make the code below in Eloquent way. I am more proficient in raw SQL though.
SELECT idx, ntiIdx, readCnt
FROM (SELECT a.idx, a.ntiIdx, a.readCnt
FROM EH_APPLICANT a
INNER JOIN EH_ITEM I ON a.netIdx = i.idx AND a.userIdx = ?
ORDER BY testDate DESC, testTime DESC
)
WHERE ROWNUM = 1
i use nhibernate that connected with oracle.
what is wrong with my syntax :
string requeteString = #"select distinct reponse1 from
(select reponse2 from CAMPAGNE reponse2 where reponse2.TEl = :tel and reponse2.EXECUTE.ID = :IdExecute order by reponse2.ReponseLEVEL desc) reponse1
where rownum = 1";
IQuery requete = this.CreateQuery(requeteString);
i got error:
NHibernate.Hql.Ast.ANTLR.QuerySyntaxException was caught
HResult=-2146232832
Message=Une exception de type 'Antlr.Runtime.NoViableAltException' a été levée. near line 2, column 37
Source=NHibernate
CreateQuery is for HQL, not SQL. You need to use CreateSQLQuery instead.
I have expression with db_link to MS SQL:
select b."Str" as "State" ,a."_Fld9059" as "Date" from
"_InfoRg9050"#SQLSERVER.UISLAB.COM a INNER JOIN
"EnumTexts"#SQLSERVER.UISLAB.COM b
on a."_Fld9052RRef" = b."_IDRRef"
where a."_Fld10998" = '1104000009' and
a."_Fld10998" = to_date(max(a."_Fld9059"),'dd.mm.yyyy')
order by a."_Fld9059" desc;
I want to upload value with maximum date. Can anybody help me ?
When I run this query I get ORA-00934 error.
The immediate cause of the error you are getting is that MAX() appears in the WHERE clause. One possible workaround, which might be what you intended, would be to use a subquery in the WHERE clause to identify the maximum date:
SELECT b.Str AS State,
a._Fld9059 AS Date
FROM _InfoRg9050 a
INNER JOIN EnumTexts b
ON a._Fld9052RRef = b._IDRRef
WHERE a._Fld10998 = '1104000009' AND
a._Fld10998 = (SELECT MAX(TO_DATE(_Fld9059, 'dd.mm.yyyy')) FROM _InfoRg9050)
ORDER BY a._Fld9059 DESC
However, it is not clear why you are comparing _InfoRg9050._Fld10998 to both the string '1104000009' and a date. You will need to resolve this on your own I believe to get a meaningful result.
Thank you for your help. I got it.
SELECT b."Str" AS "State"
FROM "_InfoRg9050"#SQLSERVER.UISLAB.COM a
INNER JOIN "EnumTexts"#SQLSERVER.UISLAB.COM b
ON a."_Fld9052RRef" = b."_IDRRef"
WHERE a."_Fld10998" = '1104000009' AND
a."_Fld9059" = (select MAX(a."_Fld9059") from "_InfoRg9050"#SQLSERVER.UISLAB.COM a
INNER JOIN "EnumTexts"#SQLSERVER.UISLAB.COM b
on a."_Fld9052RRef" = b."_IDRRef"
where a."_Fld10998" = '1104000009')
ORDER BY a."_Fld9059" DESC
This is my SQl query
select sum(stock.total_in_stock) as total_in_stock
,stock.name
,stock.inventory_id
from (
select i.store_id
,i.model_id
,i. total_in_stock
,i.id as inventory_id
, m.*
from `inventory` as `i`
left join `model_store` as `ms` on `ms`.`store_id` = `i`.`store_id`
left join `model` as `m` on `m`.`id` = `ms`.`model_id`
where `i`.`model_id` = m.id
and `m`.`status` = 1
and `ms`.`status` = 1
and `i`.`created_at` = (
select si.created_at
from inventory AS si
where si.model_id = i.model_id
and si.store_id = i.store_id
and si.status=1
order by si.created_at desc limit 1
)
) as stock
group by stock.model_id
In laravel, it is written as this:
$results1 = DB::table('inventory as i')
->select(DB::raw( 'sum(stock.total_in_stock) as total_in_stock,stock.name,stock.inventory_id FROM ( SELECT i.store_id,i.model_id,i. total_in_stock,i.id as inventory_id, m.* '))
->leftJoin('model_store as ms','ms.store_id','=','i.store_id')
->leftJoin('model as m','m.id','=','ms.model_id')
->where('i.model_id','=', 'm.id')
->where('m.status','=', '1')
->where('ms.status','=', '1')
->where("i.created_at","=",function($query) {
$query->select(DB::raw("si.created_at FROM inventory AS si WHERE si.model_id = i.model_id AND si.store_id = i.store_id AND si.status=1 ORDER BY si.created_at DESC LIMIT 1)) as stock GROUP BY stock.model_id"));
});
It gives me the following error:-
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 ')' at line 1 (SQL: select sum(stock.total_in_stock) as total_in_stock,stock.name,stock.inventory_id FROM ( SELECT i.store_id,i.model_id,i. total_in_stock,i.id as inventory_id, m.* from `inventory` as `i` left join `model_store` as `ms` on `ms`.`store_id` = `i`.`store_id` left join `model` as `m` on `m`.`id` = `ms`.`model_id` where `i`.`model_id` = m.id and `m`.`status` = 1 and `ms`.`status` = 1 and `i`.`created_at` = (select si.created_at FROM inventory AS si WHERE si.model_id = i.model_id AND si.store_id = i.store_id AND si.status=1 ORDER BY si.created_at DESC LIMIT 1 )) as stock GROUP BY stock.model_id))
It takes 2 closing brackets at the end and gives the above error. Please help me writing the above SQL query in laravel.
Answering your question that's in the tile: the subquery where needs fix:
->where("i.created_at", function($query) {
$query->from('inventory as si')
->selectRaw('max(si.created_at)')
->whereRaw('si.model_id = i.model_id AND ...');
});
However, in order to create a subquery in the from clause, you need to pass raw query, so your whole code requires a bit of toSql() and setBindings(...), which is cumbersome.
So to get the answer to your problem, better describe the problem itself instead.
Ok so the big deal is to get the rows in a mysql table that have another related row in the same table given some conditions. This table is like an activity log, so i want to notify someone that "some guy" leaved his group, but i only want do the notification when that guy joined the group before a given date, so what i do is the next sql:
SELECT ua.*, ua2.*
FROM user_activities AS ua
INNER JOIN (SELECT ua2.* FROM user_activities AS ua2
WHERE ua2.activity = "join-group"
ORDER BY ua2.created_at)
AS ua2 ON ua2.group_name = ua.group_name AND ua2.user_id = ua.user_id
WHERE ua.activity = "unjoin-group";
I omitted the date conditions due to clarity reasons.
So i need to know how to convert this to DQL (for doctrine 1.2), is it possible? or I better do it programatically?
What im trying now is this:
$q = Doctrine_Query::create()
->from('UserActivity ua, ua.User u ')
->where('ua.created_at > ?', $min_date)
->andWhere('ua.activity = ?', "unjoin-group")
->andWhereIn('u.status', array(STATUS_HOT, STATUS_ACTIVE))
->andWhere('ua.user_id IN ( SELECT
uaa.id
FROM
UserActivity uaa
WHERE
uaa.activity = ? AND
uaa.created_at < ? AND
uaa.created_at > ? AND
uaa.group_name = ua.group_name
LIMIT 1',
array("join-group", $min_date, $max_date));
But i get this error:
fatal error maximum function nesting level of '100' reached aborting
So i can't keep foward