Sorry for the basic question:
select `plans`.`name`, `A`.`subscription_id`, `A`.`amount`,
`A`.`created_date`, `A`.`end_date`,
`A`.`subscription_status`, `users`.`email`, `A`.`plan_id`,
`A`.`user_id`, `usage`.`created_at` as `usagedate`,
COUNT(usage.id) as used_count
from `subscriptions` A
left join `users` on `users`.`id` = `A`.`user_id`
left join `plans` on `A`.`plan_id` = `plans`.`Id`
left join `usage` on `A`.`user_id` = `usage`.`user_id`
where `usage`.`created_at` between A.created_at and A.end_date
group by `A`.`plan_id`
I am getting the error
1054 - Unknown column 'A.created_at' in 'where clause'
I think there should be A.created_date instead of A.created_at
select `plans`.`name`, `A`.`subscription_id`, `A`.`amount`,
`A`.`created_date`, `A`.`end_date`,
`A`.`subscription_status`, `users`.`email`, `A`.`plan_id`,
`A`.`user_id`, `usage`.`created_at` as `usagedate`, COUNT(usage.id) as
used_count from `subscriptions` A
left join `users` on `users`.`id` = `A`.`user_id`
left join `plans` on `A`.`plan_id` = `plans`.`Id`
left join `usage` on `A`.`user_id` = `usage`.`user_id`
where
`usage`.`created_at` between A.created_date and A.end_date
group by
`A`.`plan_id`
I would imagine it is meant to be a.created_date
SELECT `p`.`name`, `A`.`subscription_id`, `A`.`amount`, `A`.`created_date`, `A`.`end_date`, `A`.`subscription_status`, `u`.`email`, `A`.`plan_id`, `A`.`user_id`, `us`.`created_at` AS `usagedate`, COUNT(`us`.`id`) AS `used_count`
FROM `subscriptions` A
LEFT JOIN `users` u on `u`.`id` = `A`.`user_id`
LEFT JOIN `plans` p on `A`.`plan_id` = `p`.`Id`
LEFT JOIN `usage` us on `A`.`user_id` = `us`.`user_id`
WHERE `us`.`created_at` between `A`.`created_date` and `A`.`end_date`
GROUP BY `A`.`plan_id`
Related
I'm struggling to convert this below MySQL query into laravel query builder. It has a lot of joins and subqueries inside. Can someone please help me?
MYSQL query:
SELECT a.id , b.*, a.agent_id, td.contacts, td.addresses
FROM teams a
left join team_details as td on td.team_id = a.id
LEFT OUTER JOIN
(
SELECT m.id, m.due_date, t.team_id, m.department, m.assigned_to , cm.title, u.name
FROM (
SELECT team_id, MAX(due_date) AS due_date
FROM campaign_activities where task_status=false and due_date is not null
GROUP BY team_id
) t JOIN campaign_activities m ON m.team_id = t.team_id AND t.due_date = m.due_date
left join campaign_activity_masters cm on cm.id = m.camp_activity_id
left join users u on u.id = m.assigned_to
) b ON a.id = b.team_id
order by a.id
I'm trying something like below but got stuck with LEFT OUTER JOIN as it has sub query inside :).
DB::table('teams as a')
->leftjoin('team_details as td', 'td.team_id','=','a.id')
->select('a.id', 'b.*', 'a.agent_id','td.contacts','td.addresses')
->leftouterjoin(DB::select(DB::raw('SELECT m.id, m.due_date, t.team_id, m.department, m.assigned_to , cm.title, u.name
FROM (
SELECT team_id, MAX(due_date) AS due_date
FROM campaign_activities where task_status=false and due_date is not null
GROUP BY team_id
) t JOIN campaign_activities m ON m.team_id = t.team_id AND t.due_date = m.due_date
left join campaign_activity_masters cm on cm.id = m.camp_activity_id
left join users u on u.id = m.assigned_to')))
->get();
Any suggestions, please? I want to apply paginate function paginate() instead of get() for this query builder.
I think you should try.
$cards = DB::select("SELECT a.id , b.*, a.agent_id, td.contacts, td.addresses
FROM teams a
left join team_details as td on td.team_id = a.id
LEFT OUTER JOIN
(
SELECT m.id, m.due_date, t.team_id, m.department, m.assigned_to , cm.title, u.name
FROM (
SELECT team_id, MAX(due_date) AS due_date
FROM campaign_activities where task_status=false and due_date is not null
GROUP BY team_id
) t JOIN campaign_activities m ON m.team_id = t.team_id AND t.due_date = m.due_date
left join campaign_activity_masters cm on cm.id = m.camp_activity_id
left join users u on u.id = m.assigned_to
) b ON a.id = b.team_id
order by a.id");
Hope it help you.
I want to show the data of more than 50 tables in one list view. I have formed the query below.
Left join is dynamically added using PHP forloop.
Any other ways to get values from various tables(more than 50 tables)?
Or please suggest any other technology to use PHP Product...
SELECT * FROM main_table `t`
LEFT JOIN `table` ON table.c_application_id = t.id
LEFT JOIN `table1` ON table1.c_application_id = t.id
LEFT JOIN `table2` ON table2.c_application_id = t.id
LEFT JOIN `table3` ON table3.c_application_id = t.id
LEFT JOIN `table4` ON table4.c_application_id = t.id
LEFT JOIN `table5` ON table5.c_application_id = t.id
LEFT JOIN `table6` ON table6.c_application_id = t.id
LEFT JOIN `table7` ON table7.c_application_id = t.id
LEFT JOIN `table8` ON table8.c_application_id = t.id
LEFT JOIN `table9` ON table9.c_application_id = t.id
LEFT JOIN `table10` ON table10.c_application_id = t.id
LEFT JOIN `table11` ON table11.c_application_id = t.id
LEFT JOIN `table12` ON table12.id=t.c_cycle
LEFT JOIN `table13` ON table13.c_groupId=t.c_program_to_apply
LEFT JOIN `table14` ON table14.id = table14.c_status_id
LEFT JOIN `table15` ON table15.id = table15.c_stage_id
LEFT JOIN `table16` ON table16.c_code = table16.c_country
LEFT JOIN `table17` ON table17.id=table17.c_app_form
I have a code :
$id=implode(",",$selected);
$query = "SELECT u.id, p.brand, n.number FROM `user` u
LEFT OUTER JOIN `phone` p ON u.id = p.id LEFT OUTER JOIN `number` n
ON p.id = n.id WHERE u.id in ($id)";
Where $selected is an array array(1,2,3). But when i run it, it appears this notice :
Unknown column '1' in 'where clause'
How to handle this problem? Thank you
Here you can do it like :
$id = implode("','",$selected);
This query will run :
$query = "SELECT u.id, p.brand, n.number FROM `user` u LEFT OUTER JOIN `phone` p ON u.id = p.id LEFT OUTER JOIN `number` n
ON p.id = n.id WHERE u.id in ('$id')";
I cant figur out, why this statement does not run...
SELECT T1.* , T2.* , T3 . * , T4 . * , T5.Sessionname, T5.Session_ID, T5.Lernsession, T5.Session_Beschreibung, T5.User_ID, T6.Username, T6.ID, T7.*
FROM Session_Fragen T1, Sessions T5
INNER JOIN Fragen T4 ON T1.Frage_ID = T4.Frage_ID
INNER JOIN Semester T2 ON T2.Semester_ID = T4.Semester_ID
INNER JOIN Faecher T3 ON T3.Fach_ID = T4.Fach_ID
INNER JOIN Userdaten T6 ON T5.User_ID = T6.ID
LEFT JOIN Medien T7 ON T7.Frage_ID = T1.Frage_ID
WHERE
T1.Session_ID =1655
AND T1.User_ID =2
AND T5.Session_ID =1655
AND T4.Meldung =0
AND T4.Freigabe =1
AND T5.User_ID =2
ORDER BY T4.Fach_ID
i'm trying to solve the error: #1054 - Unknown column 'T1.Frage_ID' in 'on clause'
for couples of hours without any success...
may anybody sees what i'm doing wrong?
Please have a look at the query below - I am getting Unknown column 'u.id' in 'on clause'
SELECT id, username,
coalesce(
(SELECT name from company c
INNER JOIN user_company uc
ON uc.user_id = u.id
AND c.id = uc.company_id), 'NOT-AVAILABLE'
) companyname
FROM `user` u
Based on your comment, the correlation can't be placed within the JOIN of the correlated sub-query.
It can, however, be placed in the WHERE clause of the correlated sub-query.
SELECT
id,
username,
coalesce(
(SELECT name
FROM company c
INNER JOIN user_company uc
ON c.id = uc.company_id
WHERE uc.user_id = u.id
),
'NOT-AVAILABLE'
) companyname
FROM
`user` u
That answers your explicit question; why your query failed syntactically.
I would, however, replace the whole correlation with a simple LEFT JOIN.
SELECT
u.id,
u.username,
COALESCE(c.name, 'NOT-AVAILABLE') companyname
FROM
`user` u
LEFT JOIN
`user_company` uc
ON uc.user_id = u.id
LEFT JOIN
`company` c
ON c.id = uc.company_id
One way to fix it:
SELECT u.id,
u.username,
COALESCE(aux.name, 'NOT-AVAILABLE') as 'companyname'
FROM `user` u
LEFT JOIN
(SELECT user_id, name from company c
INNER JOIN user_company uc
ON c.id = uc.company_id) aux ON aux.user_id = u.id
Another way to fix it:
SELECT u.id,
u.username,
COALESCE(c.name, 'NOT-AVAILABLE') AS 'companyname'
FROM `user` u
LEFT JOIN user_company uc ON uc.user_id = u.id
LEFT JOIN company c ON c.id = uc.company_id