I am having some trouble fetching exact data from my database. I don't know what to do to achieve what I am looking for. I need some help in here. Here what I am facing...
Table name: ambulance
+----+---------+
| ID | CONTACT |
+----+---------+
| 01 | 1234567 |
| 02 | 2345678 |
| 03 | 3456789 |
| 04 | 4567891 |
+----+---------+
Table name: category
+----+----------+
| ID | CATEGORY |
+----+----------+
| 01 | AC |
| 02 | Non AC |
+----+----------+
Table name: multi_category
+----+--------------+-------------+
| ID | AMBULANCE_ID | CATEGORY_ID |
+----+--------------+-------------+
| 01 | 01 | 01 |
| 02 | 01 | 02 |
| 03 | 02 | 01 |
| 04 | 02 | 02 |
| 05 | 03 | 01 |
| 06 | 04 | 02 |
+----+--------------+-------------+
These are my table structures.
And now when I am applying this query
SELECT
amb.ID,
amb.CONTACT,
GROUP_CONCAT(DISTINCT cat.CATEGORY SEPARATOR ', ') AS CATG
FROM
ambulance as amb
LEFT JOIN multi_category as mct ON
amb.ID = mct.AMBULANCE_ID
LEFT JOIN category as cat ON
mct.CATEGORY_ID = cat.ID
GROUP BY
amb.ID
DESC
The result I am getting is completely okay and the result is...
+----+---------+------------+
| ID | CONTACT | CATG |
+----+---------+------------+
| 01 | 1234567 | AC, Non AC |
| 02 | 2345678 | AC, Non AC |
| 03 | 3456789 | AC |
| 04 | 4567891 | Non AC |
+----+---------+------------+
But when I am applying this query from below
SELECT
amb.ID,
amb.CONTACT,
GROUP_CONCAT(DISTINCT cat.CATEGORY SEPARATOR ', ') AS CATG
FROM
ambulance as amb
LEFT JOIN multi_category as mct ON
amb.ID = mct.AMBULANCE_ID
LEFT JOIN category as cat ON
mct.CATEGORY_ID = cat.ID
WHERE
cat.ID = 01
GROUP BY
amb.ID
DESC
I am getting this result..
+----+---------+------------+
| ID | CONTACT | CATG |
+----+---------+------------+
| 01 | 1234567 | AC |
| 02 | 2345678 | AC |
| 03 | 3456789 | AC |
+----+---------+------------+
You can see in the CATG field Non AC is missing. But ID - 01 & 02 ambulance are both AC & Non AC.
Now my question is, how do I bring back Non AC in the CATG field with the second query with a WHERE Claus?
Thanks in advance.
LEFT JOINwithWHEREon one of it's columns doesn't make sense. Did you meanLEFT JOIN...AND...or did you want anINNER JOIN?ACcategorywheretoandwould generatenullcategory for ambulance4