I have this query in mysql database:
INSERT INTO `table1`( `text1`, `text2`, `link` )
SELECT
`text1`,
`text2``,
`link`
FROM `table2`
WHERE `table1`.`code` = `table2`.`code`;
I get an error:
#1054 - Unknown column 'table1.code' in 'where clause'
What am I doing wrong? I have no aliases, I tried HAVING instead of WHERE, I tried INNER JOIN but no success. My code columns have no indices.
First of all you should show also the structure of table2 to let us help you better.
Otherwise, the error is due to the lack of table1 in your query.
As code is not present in your INSERT statement I may assume that it is an autoincrement field.
Your query (I suppose, as I can't see the definition of table2, as wrote before), could be
SELECT
t2.text1,
t2.text2,
t2.link
FROM table2 AS t2
INNER JOIN table1 AS t1 ON (t1.code = t2.code)
;
If the link between table1 and table2 is 1-to-many or many-to-many, just add a DISTINCT after the SELECT keyword to avoid duplicated results or change your query in
SELECT
t2.text1,
t2.text2,
t2.link
FROM table2 AS t2
WHERE EXISTS (
SELECT 'x'
FROM table1 AS t1
WHERE t1.code = t2.code
)
Related
I created two temp tables to join them.
create temporary table if not exists dbo.t1 as
(select * from dbo.cp where id%2=1);
create temporary table if not exists dbo.t2 as
(select * from dbo.cp where id%2=0);
I have two columns in a temporary table:
When I query
select * from dbo.t1;
this table comes up.
codes id
123 1
213 2
144 3
423 4
My issue: When i say query
select codes from dbo.t1;
I get the error: unknown column 'codes' in fieldlist.
when i query
select 'codes` from dbo.t1
I get the output
codes id
codes 1
codes 2
codes 3
codes 4
when i query
select `codes` from dbo.t1
I get the output unknown column in field list.
This is a huge issue because when I try these different queries to do an inner join I do not get the correct output:
Create Table edit AS
(select
't1.codes',
t1.id t1_id, t2.*
from t2
inner join t1 on t1.id = t2.id - 1);
Create Table edit AS
(select
t1.codes t1_codes,
t1.id t1_id, t2.*
from t2
inner join t1 on t1.id = t2.id - 1);
Create Table edit AS
(select
t1*, t2.*
from t2
inner join t1 on t1.id = t2.id - 1);
The issue here is I get the error of "duplicate column name codes"
Try to wrapped your code with backtick ( ` ) symbol
SELECT `codes` FROM ..
TICKS NOT QUOTES
select `codes` from dbo.t1;
delete ana.* from table1 as ana
where
ana.qname=(select ID from tab1 where LOCAL_NAME='xxxxx')
and exists(select 'x' from table2 an
where
ana.node_id=an.id and
an.QNAME_ID IN (select ID from tab1 where LOCAL_NAME in('bbb')));
why above query is not working in mysql.same query is working in oracle.
Error Code: 1054. Unknown column 'ana.qname' in 'where clause'
A priori, your query looks ok. I would start by formatting it and qualifying all column names:
delete ana
from table1 ana
where ana.qname = (select t1.ID
from tab1 t1
where t1.LOCAL_NAME = 'xxxxx'
) and
exists (select 1
from table2 an
where ana.node_id = an.id and
an.QNAME_ID IN (select t1.ID
from tab1 t1
where t1.LOCAL_NAME in ('bbb')
)
);
I think the error is pretty clear: table1.qname doesn't exist. I will also add that comparing something called "name" to something called "id" looks suspicious. I suspect you are simply using the wrong column name there. Perhaps the intention is qname_id.
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);
Using MySQL, I want to do all these in one insert:
table T1 contains column Ca & Cb. T1 is in database D1. Insert into table T1 specifying column Ca, Cb.
table T2 contains column C2. T2 is in database D2. Set T1.Ca's value with T2.C2
table T3 contains column C3 & C4. T3 is in database D2. use the T2.C2 value to query T3.C3 and use the C4 value of the same row to set T1.Cb
so I did:
insert into T1(Ca, Cb)
select C2
from D2.T2
union
select C4
from D2.T3
where C3=T2.C2;
Error Code: 1054. Unknown column 'T2.C2' in 'where clause'
Please help. Thank you in advance.
If i understand correcly you should use a join and not an union
insert into D1.T1(Ca, Cb)
select T2.C2, T3.C4
from D2.T2
INNER JOIN D2.T3 on T2.C2 = T3.C3
the second select of the union don't know the content of the firts select .. so you have the error
Unknown column 'T2.C2' in 'where clause'
Queries which are Unioned are independent of one another.
You want to join these tables.
select C2,C4
from D2.T2 a
INNER JOIN D2.T3 b ON b.C3=a.C2;
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