I'm trying a subquery in MySQL using max(), and I keep running into an error. The gist of the query is below (though I've changed the field names).
select table1.field1, table1.field2, table2.field3, table2.field4, table3.field5,
(select max(age)
from age_table
where age_table.person = table2.person)
from table1
inner join table2 on table2.person = table1.person
inner join table3 on table3.person = table1.person
inner join age_table on age_table.person = table1.person
When I try this, I get a syntax error that points to
'from age_table where age_table.person=table2.person'
...but I can't figure out what the problem is.
Use table aliases to differentiate between tables, without having to use the full table name:
SELECT t1.field1, t1.field2, t2.field3, t2.field4, t3.field5,
(SELECT MAX(at.age)
FROM AGE_TABLE at
WHERE at.person = t2.person) AS max_age
FROM TABLE1 t1
JOIN TABLE2 t2 ON t2.person = t1.person
JOIN TABLE3 t3 ON t3.person = t1.person
I removed what appeared to be a redundant JOIN to the AGE_TABLE, seeing as it wasn't used in the SELECT clause.
It's also good habit to define a column alias for derived column values - makes them easier to reference. See "max_age" for an example.
You need to create an alias for your subquery eg:
(select max(age) from age_table where age_table.person = table2.person) temp
and leave rest of the things as they are.
Related
this is a simplified version of my query. It runs in SQLite without an issue, but not so in MYSQL.
Error: Unknown column 'tab2.key' in 'on clause'
SELECT
tab2.key AS remarksId,
tab1.sessionDate AS date,
tab1.sessionStart AS startTime
FROM
table1 AS tab1,
table2 AS tab2,
table3 AS tab3,
table4 AS tab4
INNER JOIN
table5 AS tab5
ON
tab5.remarkId = tab2.key
WHERE
tab1.userId='<anyNumber>' AND
tab2.objectiveId='<anyKey>' AND
tab1.reportId=tab2.reportId AND
tab1.reportId=tab3.key AND
tab4.key=tab3.sortieId
ORDER BY date, startTime;
To put the columns in ` unfortunately was no help.
I'm not sure if naming a column 'key' is a good practice (MYSQL Keywords), but I have to handle this somehow, due to existing installations. For a quick test, I renamed the 'key'-columns, but also without any success.
Thanks a lot
Simply put, Don't mix join notations, and if given the choice use the current ANSI 92 join syntax instead of the 89 standard (yes... that's 1992 and 1989!)
Secondly... aliases are to help you save typing and handle multiple references to the same table.... so... simplify the names t1, t2, t3...
Lastly... if you have to use reserved/restricted words in mySQL use backticks.
SELECT t2.`key` AS remarksId
FROM table1 t1
INNER JOIN table2 t2
ON t1.reportId = t2.reportId
INNER JOIN table3 t3
ON t1.reportId = t3.`key`
INNER JOIN table4 t4
ON t4.`key` = t3.sortieId
INNER JOIN table5 t5
ON t5.remarkId = t2.`key`
WHERE t1.userId = '<anyNumber>'
AND t2.objectiveId = '<anyKey>'
ORDER BY `date`, startTime;
I am trying to use join to connect multiple tables in MS Access to get count values. But I don;t know it gives wrong count values. If I try to join them individually, then it gives me correct count values.
I have 3 Tables. Table 2 and Table 3 are independent and are connected to Table 1. Test 2 and test 3 are basically text values and I want to count the rows .
Table1(ID1(Primary Key),Name)
Table2(ID2(Primary Key), ID1(Foreign Key), Test2)
Table3 (ID3(Orimary Key), ID1(Foreign Key), Test3)
The Query that I get from MS Access is given below:
SELECT Table1. ID1, Count(Table2.Test2) AS CountOfTest2, Count(Table3.Test3) AS CountOfTest3
FROM (Table1 INNER JOIN Table2 ON Table1.ID1 = Table2.ID2)
INNER JOIN Table3 ON Table1. ID1 = Table3.ID3
GROUP BY Table1.ID1;
But this gives me wrong Count Values.
Can someone please help me.
Thanks.
When I use it individually, it gives me correct count value:
SELECT Table1. ID1, Count(Table2.Test2) AS CountOfTest2
FROM Table1 INNER JOIN Table2 ON Table1.ID1 = Table2.ID1
GROUP BY Table1.ID1;
SELECT Table1. ID1, Count(Table3.Test3) AS CountOfTest3
FROM Table1 INNER JOIN Table3 ON Table1.ID1 = Table3.ID1
GROUP BY Table1.ID1;
But when I try to join Table1, Table2 and Table 3 in MS Acces, it gives me incorrect count values.
SELECT Table1. ID1, Count(Table2.Test2) AS CountOfTest2, Count(Table3.Test3) AS CountOfTest3 FROM (Table1 INNER JOIN Table2 ON Table1.ID1 = Table2.ID1) INNER JOIN Table3 ON Table1. ID1 = Table3.ID1 GROUP BY Table1.ID1
As per my understanding it is taking the count value of 1st query in the parenthesis and multiplying it with the count values of the other inner join.
I have tried many things but don't know what to do. Access adds parenthesis for some reason.
If I can assume test2 and test 3 are unique to each record (perhaps it would be better to count the PK?)
SELECT Table1.ID1
, Count(distinct Table2.Test2) AS CountOfTest2
, Count(distinct Table3.Test3) AS CountOfTest3
FROM Table1
INNER JOIN Table2
ON Table1.ID1 = Table2.ID2
INNER JOIN Table3
ON Table1.ID1 = Table3.ID3
GROUP BY Table1.ID1;
Or you may have to get the counts before the joins though the use of inline views. You could use window functions if MSSQL SERVER but Access needs the inline views.
SELECT A.ID1
, B.CountOfTest2
, C.CountOfTest3
FROM Table1 A
INNER JOIN (SELECT Table2.ID2, count(table2.test) as CountOfTest2
FROM Table2
GROUP BY Table2.id) B
ON Table1.ID1 = B.ID2
INNER JOIN (SELECT Table3.id, count(table3.test3) as CountOfTest3
FROM Table3
GROUP BY Table3.id) C
ON B.ID1 = C.ID3
GROUP BY A.ID1;
Yup i had the same problem when i was trying to do this. just use the double sql function to counteract the html and you should be good. once the query has been doubled it will react like a C++ quota statement. If this fails you can always just quantify the source fields to adhear to the table restrictions. its actually a piece of cake i hope this helped.
I am trying to get results where CountId equals zero. I tired the code below but does not work.Can someone please please help :)
SELECT table1.id,table1.name2,table1.name3,table1.name4,table1.name, COUNT(table2.id ) AS CountId
from table1
LEFT JOIN table2
ON table1.name4 = table2.name4
AND table1.name3 = table2.name3
AND table1.name2 = table2.name2
WHERE CountId = 0
You cannot use an aggregate function (such as SUM,COUNT,etc) in WHERE clause.
Use HAVING clause after GROUP BY. Also it is a good practice to use alias names when you join multiple tables.
SELECT t1.id,t1.name2,t1.name3,t1.name4,t1.name, COUNT(t2.id) AS CountId
from table1 t1
LEFT JOIN table2 t2 ON t1.name4 = t2.name4
AND t1.name3 = t2.name3
AND t1.name2 = t2.name2
GROUP BY t1.id,t1.name2,t1.name3,t1.name4,t1.name
HAVING COUNT(t2.id) = 0
I am using this command to find the same values in two tables when the tables have 100-200 records. But When the tables have 100000-20000 records, the sql manager, browsers, shortly the computer is freesing.
Is there any alternative command for this?
SELECT
distinct
names
FROM
table1
WHERE
names in (SELECT names FROM table2)
Try with join
SELECT distinct t1.names
FROM table1 t1
join table2 t2 on t2.names = t1.names
Use EXISTS:
SELECT distinct t1.names
FROM Table1 t1
WHERE EXISTS(
SELECT 1 FROM tabl2 t2 WHERE t2.names=t1.names
)
SELECT DISTINCT t1.names
FROM table1 t1
INNER JOIN table2 t2 on t1.names=t2.names
The use of the INNER JOIN ensures that there are only exact matches returned from both tables. It should be relatively quick, but indexes may be required over the long term, especially if you're using them for other JOINs and GROUP BYs etc.
a simple join will also do it.
make sure the column is indexed.
select distinct t1.names
from table1 t1, table2 t2
where t1.names = t2.names
Show names from both tables where there is a match
SELECT names
FROM table1
UNION ALL
SELECT names
FROM table2
This query will return duplicated values if there are any. If you only want distinct values then try this but note that there will be an impact on performance
SELECT names
FROM table1
UNION
SELECT names
FROM table2
SELECT table1.names
FROM table1
INNER JOIn table2
ON table1.names = table2.names
Group By table1.names
Here is my code:
SELECT field1, field2,
(SELECT * FROM table1 WHERE a = field2),
(SELECT COUNT(*)
FROM (SELECT *
FROM table2
WHERE c = field2) as t1) as count
FROM table3;
...and here is error message:
/* SQL Error (1054): Unknown column
'field2' in 'where clause' */
I want to execute one query, to get table2's total counts.
The problem is that you're trying to use a variable declared in the outer query in the inner query. Variables are scoped the opposite way in SQL, you only have access to inner queries. What you want to do (I believe) is look for the number of table2 that have a c that matches the a from table1. This should give you that answer.
SELECT
table1.a,
table2.c,
count(*)
FROM
table1
JOIN
table2 ON table2.c = table1.a
GROUP BY
table1.a
I re-wrote your query as:
SELECT t3.field1,
t3.field2,
t1.*,
t2.cnt
FROM TABLE3 t3
LEFT JOIN TABLE1 t1 ON t1.a = t3.field2
LEFT JOIN (SELECT t.c,
COUNT(*) AS cnt
FROM TABLE2 t
GROUP BY t.c) t2 ON t2.c = t3.field2
The 1054 error is due to referencing field2 two subqueries deep - most only support one level deep.
UPDATE:
I want to edit the answer, since in MySQL 8.0 your query is already correct, derived tables can access external references no matter the level.
SELECT field1, field2,
(SELECT 'hola' FROM table1 WHERE a = field2),
(SELECT COUNT(*)
FROM (SELECT *
FROM table2
WHERE c = field2) as t1) as count
FROM table3;
"Prior to MySQL 8.0.14, a derived table cannot contain outer references. This is a MySQL restriction that is lifted in MySQL 8.0.14, not a restriction of the SQL standard. For example, the derived table dt in the following query contains a reference t1.b to the table t1 in the outer query:" https://dev.mysql.com/doc/refman/8.0/en/derived-tables.html