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

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

Related

SQL-Statement to check the role of a user in a moodle course

can I get the role_ID of a user in a course with a SQL Statemant?
I also want to know what is the role of the user in the course?
Thank you for your help!
SELECT r.id, r.shortname
FROM mdl_role_assignments AS ra
LEFT JOIN mdl_user_enrolments AS ue ON ra.userid = ue.userid
LEFT JOIN mdl_role AS r ON ra.roleid = r.id
LEFT JOIN mdl_context AS c ON c.id = ra.contextid
LEFT JOIN mdl_enrol AS e ON e.courseid = c.instanceid AND ue.enrolid = e.id
WHERE ue.userid = ? AND e.courseid = ?;

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.

PDO join statement

I have the following join below and I was wanting to know is there a better way to write it as I am getting Unknown table 'id' in MULTI DELETE and I cannot seem to pin point where.
Join:
$query = $dbConnection->prepare('
DELETE c.id, r.id, s.id,f.id,ip.id,ct.id
FROM campaigns c
JOIN campaignsFroms f ON f.id = c.id
JOIN campaignsRaw r ON r.id = c.id
JOIN campaignsSubjects s ON s.id = c.id
JOIN campaignIPTracking ip ON ip.id = c.id
JOIN campaignTracking ct ON ct.id = c.id
WHERE c.id = :campaign_id');
$query->execute(array(':campaign_id' => $campaign_id));
Your DELETE statement is incorrect. You should remove the fields.
DELETE
FROM campaigns c
JOIN campaignsFroms f ON f.id = c.id
JOIN campaignsRaw r ON r.id = c.id
JOIN campaignsSubjects s ON s.id = c.id
JOIN campaignIPTracking ip ON ip.id = c.id
JOIN campaignTracking ct ON ct.id = c.id
WHERE c.id = :campaign_id'

ERROR Mixing of Group Columns with No Group Columns

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

MYSQL customizable join and select

i want to use JOIN categories c ON c.id = i.category if post_type is 1 and i want to have c.title AS category_name, in SELECT otherwise JOIN categories c ON c.id = i.category and c.title AS category_name, must be not worked in query , i'm using case for join but my sql command is not correct. please help me. in all description my quastion means is how to change this below command to if post_type is 1 join must be not work
SELECT SQL_CALC_FOUND_ROWS
i. * ,
c.title AS category_name,
s.title AS status_title,
i.thumb_image,
CONCAT( u.name, ' ', u.family ) AS author
FROM contents i
LEFT JOIN categories c
ON i.category = CASE post_type WHEN 1 then c.id END
JOIN users u ON u.id = i.posted_by
JOIN status_topics s ON s.id = i.t_status
WHERE i.id = 2
I would always do the LEFT JOIN and put a condition on the column itself:
SELECT SQL_CALC_FOUND_ROWS
i. * ,
IF(i.post_type = 1, c.title, NULL) AS category_name,
s.title AS status_title,
i.thumb_image,
CONCAT( u.name, ' ', u.family ) AS author
FROM contents i
LEFT JOIN categories c ON i.category = c.id
JOIN users u ON u.id = i.posted_by
JOIN status_topics s ON s.id = i.t_status
WHERE i.id = 2

Categories