Zend select object - building a query - sql

How can I write the query below in Zend Select object notation so using ->join()->where() etc?
SELECT t.id, t.user_id, t.added_date, u.id, u.phone, bk.call_status
FROM `transaction` t
INNER JOIN
(
SELECT id, MAX(added_date) AS addedDate
FROM `transaction`
GROUP BY id
) gt
ON t.id = gt.id AND t.added_date = gt.addedDate
LEFT JOIN `user` u ON t.user_id = u.id
LEFT JOIN `bok_call` bk ON bk.user_id = t.user_id
WHERE NOT t.user_id = 'null'
AND addedDate BETWEEN '2008-05-04 17:51:48' AND '2009-05-04 17:51:48'
AND NOT u.phone = ''
AND bk.call_status IS null

For example this way:
$joinSelect = $model->select()
->from('transaction'), array('id', 'addedDate' => 'MAX(added_date)'))
->group('id');
$select = $model->select()
->setIntegrityCheck(false)
->from(array('t' => 'transaction'), array('id', 'user_id', 'added_date'))
->join(array('gt' => $joinSelect), 't.id = gr.id AND t.added_date = gt.addedDate', array());
->joinLeft(array('u' => 'user'), 't.user_id = u.id', array('id', 'phone'))
->joinLeft(array('bk' => 'bok_call'), 'bk.user_id = t.user_id', array('call_status'))
->where('t.user_id != ?', 'null')
->where("addedDate BETWEEN '2008-05-04 17:51:48' AND '2009-05-04 17:51:48')
->where('u.phone != ?', '')
->where('bk.call_status IS NULL');
Be aware that you won't be able to differentiate 't.id' and 'u.id' in result.

Related

How to pass a variable in join query in Laravel

I have a variable that I would like to pass in a joined RAW query. How can I do that ?
$current_user_id = Auth::user()->id;
$query = DB::table('users as u')
->selectRaw('
ROW_NUMBER() OVER (ORDER BY u.id) AS No,
u.id,
u.username,
l.level AS Rank
')
->leftjoin(DB::raw('(
SELECT
u.id as ts_user_id,
u.username as ts_username,
u.email as ts_email,
l.downline_user_id AS ts_downline_user_id ,
outer_u.username AS ts_downline_username,
sum(o.pay_amount) AS ts_team_sales
FROM
users as u
INNER JOIN user_levels AS l
ON l.upline_user_id = u.id
INNER JOIN users as outer_u
ON outer_u.id = l.downline_user_id
LEFT JOIN user_levels as downline_level
ON l.downline_user_id = downline_level.upline_user_id
INNER JOIN users as outerdownline__u
ON outerdownline__u.id = downline_level.downline_user_id
LEFT JOIN orders as o
ON o.user_id = downline_level.downline_user_id
WHERE o.status = 1 AND downline_level.level > 1 AND u.id=1 **//HOW to pass the $current_user_id variable here**
GROUP BY l.downline_user_id
)RESULT_TEAM_SALES') , 'RESULT_TEAM_SALES.ts_downline_user_id' , 'l.downline_user_id')
->where('u.id' , $current_user_id)
->groupBy('l.downline_user_id')
->get()
I would like to replace Where u.id = $current_user_id . Is passing the variable possible ?
Please advise.
Thank you.
Do something like
DB::table('users')
->join('contacts', function ($join) {
$join->on('users.column', '=', $yourVariable);
})
->get();
https://laravel.com/docs/9.x/queries
So, this is how I solved it. I made a sub query
$current_user_id = Auth::user()->id;
$teamSales= DB::table('users as u')
->selectRaw('
u.id,
u.username as ts_username,
l.downline_user_id AS ts_downline_user_id ,
outer_u.username AS ts_downline_username,
sum(o.pay_amount) AS ts_team_sales
')
->join('user_levels as l' , 'u.id' , '=' , 'l.upline_user_id')
->join('users as outer_u' , 'outer_u.id' , '=' , 'l.downline_user_id')
->leftJoin('user_levels as downline_level' , 'l.downline_user_id' , '=' , 'downline_level.upline_user_id')
->leftJoin('orders as o' , 'o.user_id' , '=' , 'downline_level.downline_user_id')
->where('o.status' , '=' , 1)
->where('downline_level.level' , '>' , 1)
->where('u.id' , '=' , $current_user_id)
->groupBy('l.downline_user_id')
;
Then used leftJoinSub method to left join the sub query
$query = DB::table('users as u')
->selectRaw('
ROW_NUMBER() OVER (ORDER BY u.id) AS No,
u.id,
u.username,
team_sales.ts_team_sales
')
->join('user_levels as l' , 'l.upline_user_id' , '=' , 'u.id')
->leftJoinSub($teamSales , 'team_sales' , function($join) {
$join->on('l.downline_user_id', '=', 'team_sales.ts_downline_user_id');
})
->where('u.id' , $current_user_id)
->groupBy('l.downline_user_id')
->get();

where clause subquery in LINQ

I am trying to convert my SQL syntax to the LINQ query , And I have a problem with a subquery.
I want to select max date-time with an Id in a table as a subquery for another select command but I don't know what's right or wrong with this below code.
what I have in Sql
select ap.* ,aph.Lat as 'personLat',aph.Lng as 'personLng',l.cityId,l.Lat as 'LifecenterLat',l.lng as 'LifecenterLng',l.Id as 'LifeCenterId'
from Applicant ap
inner join ApplicantAddressHistory aph on ap.Id = aph.ApplicantId
inner join LifePlusCenter l on aph.CityId = l.CityId
Where ap.FamilyRoleTypeId = '82e26080-fda6-4396-946c-40d0e267f1f3' and aph.CreatedDate in (select max(CreatedDate) from ApplicantAddressHistory
where ApplicantAddressHistory.ApplicantId = ap.Id)
order by ap.NationalityCode
And what I have tried in linq
var innerJoinMultipleTables = from app in db.Applicant.Where(x => x.FamilyRoleTypeId == Guid.Parse("82e26080-fda6-4396-946c-40d0e267f1f3"))
join aph in db.ApplicantAddressHistory on app.Id equals aph.ApplicantId
where aph.CreatedDate == db.ApplicantAddressHistory.Max(x => x.CreatedDate && x.Id=aph.Id)
join l in db.LifePlusCenter on aph.CityId equals l.CityId
let centerLat = l.Lat
let centerLng = l.Lng
orderby app.NationalityCode
select new { app, aph.Lat, aph.Lng, l.CityId, l.Branch, centerLat, centerLng };
without this below line of code ,my linq works correctly.
where aph.CreatedDate == db.ApplicantAddressHistory.Max(x => x.CreatedDate && x.Id=aph.Id)
I need something like this below code in Linq as a subquery
select max(CreatedDate) from ApplicantAddressHistory where ApplicantAddressHistory.ApplicantId = '9836CEC4-EDCB-492C-9899-DF4279210CD2'
I finally tried this and it workd but dont know its correct way or not?!
var innerJoinMultipleTables = from app in db.Applicant.Where(x => x.FamilyRoleTypeId == Guid.Parse("82e26080-fda6-4396-946c-40d0e267f1f3"))
join aph in db.ApplicantAddressHistory on app.Id equals aph.ApplicantId
let rept_max = (from c in db.ApplicantAddressHistory
where c.Id == app.Id
select c.CreatedDate).Max()
where aph.CreatedDate == rept_max
join l in db.LifePlusCenter on aph.CityId equals l.CityId
let centerLat = l.Lat
let centerLng = l.Lng
orderby app.NationalityCode
select new { app, aph.Lat, aph.Lng, l.CityId, l.Branch, centerLat, centerLng };
Am not sure how it works in Linq, but you can modify your query like below to get latest details as per CreatedDate for each ApplicantId.
select ap.* ,aph.Lat as 'personLat',aph.Lng as 'personLng',l.cityId,l.Lat as 'LifecenterLat',l.lng as 'LifecenterLng',l.Id as 'LifeCenterId'
from Applicant ap
inner join
(select *
from
(select *,row_number() over(partition by ApplicantId order by CreatedDate desc) rw
from ApplicantAddressHistory
) p
where p.rw=1
) aph
on ap.Id = aph.ApplicantId
inner join LifePlusCenter l
on aph.CityId = l.CityId
Where ap.FamilyRoleTypeId = '82e26080-fda6-4396-946c-40d0e267f1f3'
/*
and aph.CreatedDate in (
select max(CreatedDate) from ApplicantAddressHistory
where ApplicantAddressHistory.ApplicantId = ap.Id
)
*/
order by ap.NationalityCode
Finally I came up to :
var z = from app in db.Applicant
join aph in db.ApplicantAddressHistory on app.Id equals aph.ApplicantId
join l in db.LifePlusCenter on aph.CityId equals l.CityId
let createDate = app.ApplicantAddressHistory.Max(c => c.CreatedDate)
where createDate.HasValue && aph.CreatedDate == createDate
&& app.FamilyRoleTypeId == Guid.Parse("82e26080-fda6-4396-946c-40d0e267f1f3")
let personLat = aph.Lat
let personLng = aph.Lng
let centerId = l.Id
let LifecenterLat = l.Lat
let LifecenterLng = l.Lng
orderby app.NationalityCode
select new { app, aph, l, app.Id, l.CityId, centerId, personLat, personLng, LifecenterLat, LifecenterLng };

Two queries return different data

I am running two queries according to me both should return same result, but i think i am missing something
SELECT "id", "email", "first_name", "last_name", locale
FROM "users" AS "users"
WHERE (
EXISTS (
SELECT cf.field_value, ucf.field_value
FROM custom_fields cf
LEFT JOIN users_custom_fields ucf ON cf."id" = ucf.custom_field_id
WHERE cf."id" = 272 AND
((
cf.field_value = 'true') OR
(
ucf.user_id = users.id AND ucf.field_value = 'true'))
))
it returns record
SELECT users.id, cf.field_value, ucf.field_value, ucf.user_id
FROM users
Join custom_fields cf on cf.user_id = users.id
LEFT join users_custom_fields ucf on ucf.custom_field_id = cf."id"
WHERE cf."id" = 272 AND
((
cf.field_value = 'true') OR
(ucf.user_id = users.id AND ucf.field_value = 'true'))
and it doesn't return anything, is there any difference between these two?
your second query you also join custom_fields table with cf.user_id = users.id. first query does not have this.
With adding cf.user_id = users.id, your first query equal the second.
SELECT "id", "email", "first_name", "last_name", locale FROM "users" AS "users" WHERE (
EXISTS (
SELECT cf.field_value, ucf.field_value
FROM custom_fields cf
LEFT JOIN users_custom_fields ucf ON cf."id" = ucf.custom_field_id
WHERE cf."id" = 272
AND cf.user_id = users.id
AND (( cf.field_value = 'true') OR
( ucf.user_id = users.id AND ucf.field_value = 'true'))
))

Convert SQL query with inner, left, and nested select to LINQ

how can convert this query to linq?
SELECT p.PostID, p.PostText, p.PublishDate, u.Name
FROM AspNetUsers u INNER JOIN Posts p ON u.Id = p.PostUserID LEFT JOIN Reposts r ON p.PostID = r.PostID
WHERE p.PostUserID = 'id'
OR p.PostUserID IN ( SELECT FollowingUserID FROM Friends WHERE FollowerUserID = 'id' AND isUnfollow = 0)
OR p.PostID in (SELECT PostID FROM Reposts WHERE RepostUserID = 'id' OR RepostUserID IN ( SELECT FollowingUserID FROM Friends WHERE FollowerUserID = 'id' AND isUnfollow = 0))
ORDER BY p.PostUserID
Does this work?
var results = from u in AspNetUsers
join p in Posts on u.Id equals p.PostUserID
join r in Reposts on p.PostId equals r.PostId into records
from record in records.DefaultIfEmpty()
where p.PostUserID == "id"
|| (from friend in Friends
where friend.FollowerUserID == "id"
&& friend.isUnfollow == 0
select friend.FollowingUserID
).Contains(p.PostUserID)
|| (from r2 in Reposts
where r2.RepostUserID == "id"
|| (from friend2 in Friends
where friend2.FollowerUserID == "id"
&& friend2.isUnfollow == 0
select friend2.FollowingUserID
).Contains(r2.RepostUserID)
select r2.PostID
).Contains(p.PostID)
orderby p.PostUserID ascending
select p.PostID, p.PostText, p.PublishDate, u.Name;

Complex SQL to LINQ conversion with subquery

I am trying to convert this expression into LINQ from SQL, but a bit too difficult for me, maybe you can help me with this!
SELECT TOP (2) RecipeID, UserID, Name, Servings, PreparationTime, TotalTime, DifficultyLevelID, CuisineID, DishID, MainIngredientID, PriceLevelID, FlavourID, Instructions,
Notes, Thumbnail, VideoLink
FROM dbo.Recipes
WHERE (RecipeID NOT IN
(SELECT DISTINCT Recipes_1.RecipeID
FROM dbo.Allergies INNER JOIN
dbo.UsersAllergies ON dbo.Allergies.AllergyID = dbo.UsersAllergies.AllergyID INNER JOIN
dbo.IngredientsAllergies ON dbo.Allergies.AllergyID = dbo.IngredientsAllergies.AllergyID INNER JOIN
dbo.Ingredients ON dbo.IngredientsAllergies.IngredientID = dbo.Ingredients.IngredientID INNER JOIN
dbo.RecipesIngredients ON dbo.Ingredients.IngredientID = dbo.RecipesIngredients.IngredientID INNER JOIN
dbo.Recipes AS Recipes_1 ON dbo.RecipesIngredients.RecipeID = Recipes_1.RecipeID INNER JOIN
dbo.Users ON dbo.UsersAllergies.UserID = dbo.Users.UserID INNER JOIN
dbo.AllergyFactors ON dbo.IngredientsAllergies.AllergyFactorID = dbo.AllergyFactors.AllergyFactorID
WHERE (dbo.Users.UserID = 3) AND (dbo.AllergyFactors.AllergyFactorID < 3)))
It would be easier to help you if you showed us what you have already tried, but a Linq expression like this should give you the same result set
var query = (from rec in context.Recipes
where !(from al in context.Allergies
from ua in context.UsersAllergies.Where(x => al.AllergyID == x.AllergyID)
from ia in context.IngredientsAllergies.Where(x => al.AllergyID == x.AllergyID)
from in in context.Ingredients.Where(x => ia.IngredientID == x.IngredientID)
from ri in context.RecipesIngredients.Where(x => in.IngredientID == x.IngredientID)
from re in context.Recipes.Where(x => ri.RecipeID == x.RecipeID)
from us in context.Users.Where(x => ua.UserID == x.UserID)
from af in context.AllergyFactors.Where(x => ia.AllergyFactorID == x.AllergyFactorID)
where us.UserID == 3 && af.AllergyFactorID < 3
select re.RecipeID)
.Distinct()
.Contains(rec.RecipeID)
select new
{
rec.RecipeID,
rec.UserID,
rec.Name,
rec.Servings,
rec.PreparationTime,
rec.TotalTime,
rec.DifficultyLevelID,
rec.CuisineID,
rec.DishID,
rec.MainIngredientID,
rec.PriceLevelID,
rec.FlavourID,
rec.Instructions,
rec.Notes,
rec.Thumbnail,
rec.VideoLink
}).Take(2);