How to make joins in cases in mysql

132
December 22, 2018, at 04:40 AM

I am trying to construct the following query, I know the syntax is incorrect but could anyone suggest any ideas?/solution?

Thanks 
SELECT * FROM table 
LEFT JOIN x ON x.id = table.id 
CASE table.nid 
WHEN 1 THEN (LEFT JOIN tablea ON tablea.id = table.nid) 
WHEN 2 THEN (LEFT JOIN tableb ON tableb.id = table.nid) 
WHEN 3 THEN (LEFT JOIN tablec ON tablec.id = table.nid) 
END 
WHERE talbe.id = "hello";
Answer 1

i'm not sure this is the most efficient/pretty ways but you can try this :

SELECT CASE table.nid WHEN 1 THEN tablea.id WHEN 2 THEN tableb.id WHEN 3 THEN tablec.id end
FROM table
LEFT JOIN x ON x.id = table.id 
LEFT JOIN tablea ON tablea.id = table.nid
LEFT JOIN tableb ON tableb.id = table.nid
LEFT JOIN tablec ON tablec.id = table.nid 
WHERE talbe.id = "hello";
Rent Charter Buses Company
READ ALSO
Build table with rowspan using multidimensional array

Build table with rowspan using multidimensional array

I want students and events for week to be displayed in calendar formatI have this array as follows

109
PHP MySqli Can't download file from the database. Failed to load PDF document

PHP MySqli Can't download file from the database. Failed to load PDF document

I'm trying to download a file from my database which i have uploaded using path and copy the file to a folderWhen i download the file and try to open it it says "Failed to load PDF document"

110
Unable to fix the code for cohort analysis in MYSQL

Unable to fix the code for cohort analysis in MYSQL

I tried to do the following: I grouped the orders into buckets based on date and then tried to create a pivot tableHowever, I do not get the output as expected

108