I want something like this, not sure if my syntax is correct though. I will be executing this with php.
SELECT
a.column1,
a.column2,
b.column1,
c.column1,
IF a.column3 NOT NULL
THEN (
SELECT c.column1, c.column2, c.column3, d.column1
FROM table_d d
INNER JOIN table_c c
ON d.column1 = c.column1 AND c.column4 = 1
WHERE d.column2 = a.column3
);
END IF;
FROM table_a a
INNER JOIN table_b b
ON a.column1 = b.column1 AND b.column2 = 1
INNER JOIN table_c c
ON a.column1 = c.column1 AND c.column2 = 1
WHERE
a.column1 = 1000
AND b.column3 = 1
AND c.column3 = 0
ORDER BY
a.column1 ASC
So the output will be something like this:
It would be ok if it has multiple rows with the same data on the first few columns. Something like this:
grey area is from the outer SELECT and white area is from the inner SELECT
Note that both outer and inner select statement has table_c. If without the IF statement, can I do this?
SELECT
a.column1,
a.column2,
b.column1,
c.column1,
cc.column1,
cc.column2,
cc.column3,
d.column1
FROM table_a a
INNER JOIN table_b b
ON a.column1 = b.column1 AND b.column2 = 1
INNER JOIN table_c c
ON a.column1 = c.column1 AND c.column2 = 1
LEFT JOIN table_d d
ON a.column3 = d.column2
INNER JOIN table_c cc
ON d.column1 = cc.column1 AND cc.column4 = 1
WHERE
a.column1 = 1000
AND b.column3 = 1
AND c.column3 = 0
ORDER BY
a.column1 ASC
It kinda feels wrong to me.
What if I use fetch_assoc?
Is it even possible to do this in one query?


c.column1 AS c_col1? like this? so if i do this the second code snippet will work?fetch_assocto get all the fields.