ERROR Mixing of Group Columns with No Group Columns - php

I got this error and I'm not sure how to work with it. Simple explanations would be much appreciated. Error:
Error Number: 1140
Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
SELECT a.nim,b.nama_mahasiswa,c.j_sks,b.jurusan,b.kelas_program,e.status from tbl_dosen_wali a left join tbl_mahasiswa b on a.nim=b.nim left join (select k.nim,k.kd_jadwal,SUM(l.jum_sks) as j_sks from tbl_perwalian_detail k left join (select x.kd_jadwal, y.jum_sks from tbl_jadwal x left join tbl_mk y on x.kd_mk=y.kd_mk) as l on k.kd_jadwal=l.kd_jadwal) c on a.nim=c.nim left join tbl_perwalian_header e on a.nim=e.nim where a.kd_dosen='D001' group by a.nim
Filename: C:\xampp\htdocs\krs\siakad\system\database\DB_driver.php
Line Number: 330
Formatted Query:
SELECT a.nim,
b.nama_mahasiswa,
c.j_sks,
b.jurusan,
b.kelas_program,
e.status
FROM tbl_dosen_wali a
LEFT JOIN tbl_mahasiswa b
ON a.nim = b.nim
LEFT JOIN (SELECT k.nim,
k.kd_jadwal,
Sum(l.jum_sks) AS j_sks
FROM tbl_perwalian_detail k
LEFT JOIN (SELECT x.kd_jadwal,
y.jum_sks
FROM tbl_jadwal x
LEFT JOIN tbl_mk y
ON x.kd_mk = y.kd_mk) AS l
ON k.kd_jadwal = l.kd_jadwal) c
ON a.nim = c.nim
LEFT JOIN tbl_perwalian_header e
ON a.nim = e.nim
WHERE a.kd_dosen = 'D001'
GROUP BY a.nim

as the error said you need to use GROUP BY in your subquery.
try this
SELECT a.nim,
b.nama_mahasiswa,
c.j_sks,
b.jurusan,
b.kelas_program,
e.status
FROM tbl_dosen_wali a
LEFT JOIN tbl_mahasiswa b
ON a.nim = b.nim
LEFT JOIN (SELECT k.nim,
k.kd_jadwal,
Sum(l.jum_sks) AS j_sks
FROM tbl_perwalian_detail k
LEFT JOIN (SELECT x.kd_jadwal,
y.jum_sks
FROM tbl_jadwal x
LEFT JOIN tbl_mk y
ON x.kd_mk = y.kd_mk) AS l
ON k.kd_jadwal = l.kd_jadwal
GROUP BY k.nim
^^^^^^^^^^^^^^^^
) c
ON a.nim = c.nim
LEFT JOIN tbl_perwalian_header e
ON a.nim = e.nim
WHERE a.kd_dosen = 'D001'
GROUP BY a.nim

Related

mySql LEFT JOIN not returning data as expected when Condition by ID php variable

When I use the PHP $qs variable in the WHERE condition it works fine but in LEFT JOIN condition, it's not returning any data.
Problem Line: AND e.terms_id_user =
My Query: (Working)
SELECT
*,
'users_staff_driving_license' AS driving_license_link,
DATE_FORMAT(terms_date_created,'%a %d-%M-%Y %r') AS udate_created,
DATE_FORMAT(terms_date_updated,'%a %d-%M-%Y %r') AS udate_updated
FROM users AS a
LEFT JOIN users_staff_details AS b
ON a.user_id = b.users_staff_id_user
LEFT JOIN user_access AS c
ON a.user_id_access_level = c.user_access_id
LEFT JOIN businesses AS d
ON a.user_id_client = d.business_id
LEFT JOIN terms AS e
ON e.terms_id_client = 'AA0000001'
AND e.terms_id_store = '1'
AND e.terms_id_user = 1001
AND e.terms_datatype = 'users_staff_driving_license'
WHERE
a.user_id = '$qs'
My Query: (Not Working)
SELECT
*,
'users_staff_driving_license' AS driving_license_link,
DATE_FORMAT(terms_date_created,'%a %d-%M-%Y %r') AS udate_created,
DATE_FORMAT(terms_date_updated,'%a %d-%M-%Y %r') AS udate_updated
FROM users AS a
LEFT JOIN users_staff_details AS b
ON a.user_id = b.users_staff_id_user
LEFT JOIN user_access AS c
ON a.user_id_access_level = c.user_access_id
LEFT JOIN businesses AS d
ON a.user_id_client = d.business_id
LEFT JOIN terms AS e
ON e.terms_id_client = 'AA0000001'
AND e.terms_id_store = '1'
AND e.terms_id_user = '$qs'
AND e.terms_datatype = 'users_staff_driving_license'
WHERE
a.user_id = '$qs'

I'm not able to convert the Mysql query into laravel query builder

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.

How do i use CASE in mysql select query with left join?

select r.email,
rc.cuisine_id,
rc.restaurant_id,
c.id as c_id ,
c.name as cus_name,
r.image,
r.web_address,
r.area,
rp.TypeId,
rp.PaymentStatus ,
rp.RestaurantId
from restaurant as r
left join restaurant_cuisine as rc on r.id=rc.restaurant_id
left join cuisine as c on rc.cuisine_id=c.id where c.status='1'
Case statement that is needed
CASE WHEN rp.TypeId = '1'
THEN left join restaurant_payment as rp on r.id=rp.RestaurantId
WHEN rp.TypeId = '3'
THEN left join users as u on rp.RestaurantId=u.id
END
Try this
select r.email,
rc.cuisine_id,
rc.restaurant_id,
c.id as c_id ,
c.name as cus_name,
r.image,
r.web_address,
r.area,
rp.TypeId,
rp.PaymentStatus ,
rp.RestaurantId
from restaurant as r
left join restaurant_cuisine as rc on r.id=rc.restaurant_id
left join cuisine as c on rc.cuisine_id=c.id where c.status='1' and
rp.TypeId = CASE
WHEN rp.TypeId = '1'
THEN left join restaurant_payment as rp on r.id=rp.RestaurantId
WHEN rp.TypeId = '3'
THEN left join users as u on rp.RestaurantId=u.id
END

GROUP_CONCAT too slow

Hello I added GROUP_CONCAT function to my query and that function killed my query :/.
My query is :
SELECT u.username,a.user_id,a.id,a.text,a.lang as fromLang,b.lang as toLang,GROUP_CONCAT(DISTINCT b.id) AS translation_ids FROM sentence as a
INNER JOIN sentence_relationship as sr ON
(sr.sentence_id = a.id)
INNER JOIN sentence as b ON
(b.id = sr.translation_id AND a.id = sr.sentence_id)
INNER JOIN users as u ON
(u.id = a.user_id) GROUP BY a.id LIMIT 10;
What is wrong with that query ?
This is your query (somewhat formatted):
SELECT u.username, a.user_id, a.id,a.text,a.lang as fromLang, b.lang as toLang,
GROUP_CONCAT(DISTINCT b.id) AS translation_ids
FROM sentence a INNER JOIN
sentence_relationship sr
ON sr.sentence_id = a.id INNER JOIN
sentence b
ON b.id = sr.translation_id AND a.id = sr.sentence_id INNER JOIN
users as u
ON u.id = a.user_id
GROUP BY a.id
LIMIT 10;
It is unclear from your question whether the group_concat() was added with the group by. That could slow things down.
The limit 10 is taking the first 10 a.ids that match (the group by does an implicit ordering). If you do this with a subquery, it will probably speed up the query:
SELECT u.username, a.user_id, a.id,a.text,a.lang as fromLang, b.lang as toLang,
GROUP_CONCAT(DISTINCT b.id) AS translation_ids
FROM (select s.*
from sentence s
order by a.id
limit 10
) a INNER JOIN
sentence_relationship sr
ON sr.sentence_id = a.id INNER JOIN
sentence b
ON b.id = sr.translation_id AND a.id = sr.sentence_id INNER JOIN
users as u
ON u.id = a.user_id
GROUP BY a.id;
This assumes that all the joins do work and match records. If the joins are used for filtering, then you may get fewer than 10 rows back.

MYSQL LEFT JOIN Multiple Tables Twice

I have following MySQL Code:
SELECT *
FROM b_movies, b_mov_rol_celeb, b_starcast
LEFT JOIN bb_celebs ON b_mov_rol_celeb.celeb_id = bb_celebs.celeb_id
OR b_starcast.celeb_id = bb_celebs.celeb_id
LEFT JOIN bb_roles ON b_mov_rol_celeb.role_id = bb_roles.role_id
WHERE b_movies.id = '14';
Its giving error:
Unknown column 'b_mov_rol_celeb.celeb_id' in 'on clause'
Try this:
SELECT m.title_slug, m.title, m.release_date,
c.celeb_slug, c.celeb_name,
r.role_name,
s.char_name
FROM b_movies m
INNER JOIN b_starcast s ON m.id = s.movie_id
INNER JOIN b_mov_rol_celeb mrc ON m.id = mrc.movie_id
LEFT JOIN bb_celebs c ON mrc.celeb_id = c.celeb_id OR s.celeb_id = c.celeb_id
LEFT JOIN bb_roles r ON mrc.role_id = r.role_id
WHERE m.id = '14';
The order of the tables matters in a left or right join.

Categories