CodeGuy
CodeGuy

Reputation: 28905

Joining two tables in SQL

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

Upvotes: 1

Views: 135

Answers (2)

Robert
Robert

Reputation: 25763

Try this:

select Z.C /*other*/ 
from (SELECT   E.C AS C, 
               COUNT(E.C) AS CC
      FROM     E
      GROUP BY E.C) Z

JOIN (SELECT   E.C AS C, 
             COUNT(E.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) Y on Y.C = Z.C

Upvotes: 1

Taryn
Taryn

Reputation: 247860

You did not show your syntax error but, I am guessing your has to do with the following line:

COUNT(C) AS CC

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:

select t1.C, t1.cc, t2.num
from 
(
    SELECT   E.C AS C, 
             COUNT(C) AS CC
    FROM     E
    GROUP BY E.C
) t1  -- added alias
INNER JOIN
(
    SELECT   E.C AS C, 
             COUNT(E.C) AS Num  -- added table name, since column c exists in both tables
    FROM    E 
    JOIN    G
        ON E.G = G.L
    WHERE  G.G < (SELECT min(G.G) 
                   FROM   G
                   WHERE  G.L = "BLAH")
    GROUP BY E.C
) t2  -- added alias
    on t1.C = t2.C  -- added on clause

Upvotes: 1

Related Questions