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
Related
select distinct column1
from Table1
where Table1id in ((select T2.Table1id
from Table2 T2
where (conditions)
order by T2.column)
limit 2
);
I cannot use limit inside the In operator. Do we have any other way to limit inside IN operator? Or do we have any other way without using IN and also without using any joins?
Error (while using limit inside the In Operator):-
Error Code: 1235. This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
For non-specific MySQL versions, Ergest gave you a good solution of using JOIN. Here is another workaround in which an outer layer is used on top of the derived table.
select distinct column1
from Table1
where Table1id in (select id
from
(select T2.Table1id as id
from Table2 T2
where (conditions)
order by T2.column
limit 2) tb);
PS: this trick can be used to bypass the ERROR 1093 (HY000): You can't specify target table 'terms' for update in FROM clause
What a weird restriction. Well, you can simply use an ad-hoc view (aka. WITH clause or CTE):
with limited as
(
select T2.Table1id
from Table2 T2
where (conditions)
order by T2.column
limit 2
)
select distinct column1
from Table1
where Table1id in (select Table1id from limited);
Demo: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=cc148fae3a1089324446ec792e1476e2
Let's turn it inside out:
select distinct T1.column1
FROM
( SELECT T2.Table1id
from Table2 T2
where (conditions)
order by T2.column
limit 2
) AS x
JOIN Table1 AS T1 ON T1.table1id = x.Table1id
The inner (derived) table may need DISTINCT.
I'm trying to convert this MySQL query to SQL Server, but I do not know much about SQL Server
SELECT
*
FROM
Table1 AS T1
INNER JOIN Table2 AS T2
ON T1.Column1 = T2.ColumnX
WHERE
T1.ColumnY = 'xxxx'
GROUP BY
T1.Column1
Somebody can help me?
Your query is just an erroneous query, because you are using select * with group by. This query uses a MySQL extension. And, the default settings in more recent versions of MySQL would generate an error.
Here is one method for converting this to SQL Server:
SELECT TOP (1) WITH TIES *
FROM Table1 AS T1 INNER JOIN
Table2 AS T2
ON T1.Column1 = T2.ColumnX
WHERE T1.ColumnY = 'xxxx'
ORDER BY ROW_NUMBER() OVER (PARTITION BY T1.Column1 ORDER BY (SELECT NULL)) ;
Probably a better method (from a performance perspective) uses a lateral join:
SELECT *
FROM Table1 T1 CROSS APPLY
(SELECT TOP (1) T2.*
FROM Table2 T2
WHERE T1.Column1 = T2.ColumnX
) T2
WHERE T1.ColumnY = 'xxxx' ;
Both of these choose arbitrary rows from Table2 when there is more than one match.
I'd tried insert with multiple statement with the code below
insert into peoplePos
select a.name,b.option
FROM (SELECT name from people t1) a
JOIN (SELECT option FROM optionTable WHERE name = 'Position') b
where not exists (select * from peoplePos t2 where t2.name = t1.name);
However i got this error
Error Code: 1054. Unknown column 't1.name' in 'where clause'
It seems in the where clause, t1 cant access the people t1 declared earlier
I'd tried using a.t1.name and people.name, both doesn't work
Is there away to access it? Thanks
I'd think you'd want some conditions on your join, but for what you ask, t1 does not exist. You have aliased it as a so use a.
insert into peoplePos
select a.name,b.option
FROM (SELECT name from people t1) a
JOIN (SELECT option FROM optionTable WHERE name = 'Position') b ON "SOME CONDITION OR OTHER"
where not exists (select * from peoplePos t2 where t2.name = a.name);
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
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.