I have two tables that share the same column (C) but SQL tells me I have an error
Here is the first table from the first query:
SELECT E.C AS C,
COUNT(C) AS CC
FROM E
GROUP BY E.C
And here is the second query:
SELECT E.C AS C,
COUNT(C) AS Num
FROM E, G
WHERE E.G = G.L
AND G.G <
(SELECT min(G.G)
FROM G
WHERE G.L = "BLAH")
GROUP BY E.C
So I tried to simply put these two together by putting JOIN in between them. But this doesn’t join them, even though they share the same column C. It simply says I have a syntax error. What is wrong? And how do I fix it? I have confirmed that when run separately, they produce the correct output with a column C
Below is the total JOIN execution
(SELECT E.C AS C,
COUNT(C) AS CC
FROM E
GROUP BY E.C)
JOIN
(SELECT E.C AS C,
COUNT(C) AS Num
FROM E, G
WHERE E.G = G.L
AND G.G <
(SELECT min(G.G)
FROM G
WHERE G.L = "BLAH")
GROUP BY E.C)
And the error is
Error code 1064, SQL state 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘JOIN
(SELECT E.C AS C,
COUNT(C) AS Num' at line 6
Line 1, column 1
You did not show your syntax error but, I am guessing your has to do with the following line:
Since both tables have a column
C, the query does not know which table to return the value from. You need to preface it with the table name or alias.This line works in your first query because you only had one column called
C, once you have two you need to specify which one you want to return.Based on your edit, you are missing aliases for the subqueries and the on condition for the join: