Joining two tables in SQL - mysql

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:
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

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

Related

UPDATE VALUE IN MYSQL MARIA DB WITH JOIN

I am trying to change the value of a column table with a sum function, this is my code:
For example
c.total = (10-2-3) - (3)
c.total = 2
update tabC c
JOIN tabB b ON b.c_id = c.id
set c.total = (c.v1 - c.v2 - c.v3) - IF(sum(b.payment) is not null, sum(b.payment), 0)
where c.id= 983;
but I get the following error:
ERROR 1111 (HY000): Invalid use of group function
I think the error is sum, but how can I solve that?
Thanls in advance
You need to join with a subquery that uses GROUP BY.
UPDATE tabC c
LEFT JOIN (
SELECT c_id, SUM(payment) AS total_payment
FROM tabB
GROUP BY c_id) AS b ON b.c_id = c.id
SET c.total = (c.v1 - c.v2 - c.v3) - IFNULL(b.total_payment, 0)
WHERE c.id = 983
You should usee a nested subquery whit aggregated resutl for join
update tabC c
JOIN (
select c_id, ifnull(sum(payment),0) tot_payment
from tabB
group by c_id
) b ON b.c_id = c.id
set c.total = (c.v1 - c.v2 - c.v3) - b.tot_payment
where c.id= 983;

Unknown column 'b.Hostname' in 'on clause'

I have the following MYSQL Query - trying to join 3 tables and find unique information(data)
SELECT a.LocationID, a.Model, a.SerialNum,a.Purpose, b.IP, a.Services,a.DeviceID, COUNT(a.Hostname)
FROM RefConnection.Equipment_Info a, RefConnection.Connections b, RefConnection.VM_Info c
JOIN Equipment_Info on b.Hostname = a.Hostname
WHERE a.Hostname = c.Hostname
AND b.status = a.Status
AND a.status = c.Status
GROUP BY a.LocationID, a.Model, a.SerialNum, a.Purpose, b.IP, a.Services, a.DeviceID
ORDER BY COUNT(b.Hostname)DESC;
This works with 2 tables :
SELECT d.locationID, d.Model, d.SerialNum, d.Status, da.IP, COUNT(d.HOSTNAME)
FROM RefConnection.Equipment_Info d, RefConnection.Connections da
WHERE d.Hostname = da.Hostname
AND d.Status = da.Status
Group By d.locationID, d.Model, d.locationID, d.Model, d.SerialNum, d.Status, da.IP
ORDER BY COUNT(da.Hostname) DESC;
Unknown column 'b.Hostname' in 'on clause'
Never use commas in the FROM clause. Always use proper, explicit, standard JOIN syntax.
FROM RefConnection.Equipment_Info a JOIN
RefConnection.Connections b
ON b.Hostname = a.Hostname AND
b.status = a.Status JOIN
RefConnection.VM_Info c
ON a.Hostname = c.Hostname AND
a.status = c.Status
Your problem is the scoping rules for identifiers. This does not work as you expect with commas -- yet another reason to avoid them.
I don't know what Equipment_Info is doing a second time in the query.
I would also strongly advise you to use meaningful table aliases, such as e, c, and i rather than arbitrary characters.

Only show instances that equal the max value in temp table

I'm trying to create a new table with only the sname's that equal the maximum number of num_courses which is a column in a temporary table n.
SELECT s.sname, n.num_courses
FROM (SELECT e.sid, COUNT(distinct e.cno) as num_courses
FROM enroll e
GROUP BY e.sid) n, student s
WHERE s.sid = n.sid AND n.num_courses = (SELECT MAX(n.num_course) from n) x;
Is it possible to only show instances that equal the max value found in a temporary table? (Referring to the second WHERE clause on the last line)
This is the error:
ERROR 1064 (42000) at line 1 in file: 'q7.sql': 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 'x' at line 5
It says the error is at line 1 but when I remove the last clause on the last line, there's no error.
You can't reuse the subquery like that. You'll need to write the query again in the where clause.
I think you intended to do this:
select s.sname,
n.num_courses
from (
select e.sid,
COUNT(distinct e.cno) as num_courses
from enroll e
group by e.sid
) n,
student s
where s.sid = n.sid
and n.num_courses = (
select MAX(n.num_course)
from (
select COUNT(distinct e.cno) as num_courses
from enroll e
group by e.sid
) t
);
You don't need x tablename and you can't use the n table alias but you need all the code
SELECT s.sname, n.num_courses
FROM (SELECT e.sid, COUNT(distinct e.cno) as num_courses
FROM enroll e
GROUP BY e.sid) n
INNER JOIN student s ON s.sid = n.sid AND (SELECT MAX(n.num_course) from
( SELECT e.sid, COUNT(distinct e.cno) as num_courses
FROM enroll e
GROUP BY e.sid) n )
You don't need that alias in WHERE condition and also try using IN instead since MAX() could return multiple result like
n.num_courses IN (SELECT MAX(n.num_course) from n);

Is the syntax for join on stricter than using comma where?

The following query fails to be parsed:
select t.ename, t.received, d.loc from
(
select e.ename, eb.received , e.deptno
from emp e left outer join emp_bonus eb
on
e.empno=eb.empno
)
as t
join dept as d on d.deptno = t.deptno;
with the error:
Column 'deptno' in field list is ambiguous
But this query is parsed succesfully:
select t.ename, t.received, d.loc from
(
select e.ename, eb.received , e.deptno
from emp e left outer join emp_bonus eb
on
e.empno=eb.empno
)
as t, dept as d where d.deptno = t.deptno
I only changed the JOIN ON to t, dept where
Why does the first version fail?
the first query runs perfectly in mysql version 5.1
see the demo

What is this type of table called?

SELECT
*
FROM
(
SELECT a, b, c
FROM ABC
WHERE f = '1'
LEFT JOIN
SELECT v, n, m
FROM VBN
WHERE g = '1'
ON (v = a)
);
I am trying to select from a table is is built on the fly from conditions, what is this kind of table called? Live table?
You can call it as subquery.
If you wanted to apply a name to this subquery you can use an alias or by creating a view.
SELECT
aJoinedTable.*
FROM
(
SELECT a, b, c
FROM ABC
WHERE f = '1'
LEFT JOIN
SELECT v, n, m
FROM VBN
WHERE g = '1'
ON (v = a)
) as aJoinedTable;