Error Code: 1054. Unknown column '' in 'where clause' - mysql

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.

Related

#1054 - Unknown column in 'where clause'

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
)

Mysql UPDATE Query error: Unknown Column in 'field list'

I am trying to add a new column by using the following query:
UPDATE t1
INNER JOIN
(SELECT
t1.CID as t1_id,
t2.id as t2_id
FROM t3
INNER JOIN t1 ON t3.CID = t1.CID
INNER JOIN t4 ON t4.MID = t3.MID
INNER JOIN t2 ON t2.serial = t4.Serial
AND t3.Time BETWEEN t2.Start_Time AND t2.End_Time) as sub
ON sub.t1_id = t1.CID
SET t1.t2_id = sub.t2_id
Shows the error: Unknown column 't1.t2_ID' in 'field list'
I have seen couple of fieldlist error solutions but none of them actually helped me. So any help will be appreciated.
If you want add a column use ALTER TABLE
ALTER TABLE table
ADD [COLUMN] column_name column_definition [FIRST|AFTER existing_column];
http://www.mysqltutorial.org/mysql-add-column/
Then you can use UPDATE to fill that field

Insert with multiple select statement if not exist

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);

SQL query from multiple table with condition

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;

Update MySQL table column from another table entities

I have 2 tables and I need to update a column from table2 using a column in table1. table2.id2 is empty and I must fill it using table1.id. Also, you must know that I have 2 columns that can be matched with each other in these tables (table1.code and table2.code).This is my SQL :
UPDATE table2 SET table2.id2 = table1.id WHERE table2.code = table1.code;
Is this query right ? I'm getting this error, while I'm sure that table1.code exists.
[Err] 1054 - Unknown column 'table1.code' in 'where clause'
Assuming you can join both tables using code
UPDATE T2
JOIN T1 ON T1.CODE = T2.CODE
SET
T2.ID2 = T1.ID
WHERE
T2.ID2 = '';
Doesn't work that way.
Take this:
UPDATE table2 SET id2 = (SELECT id from table1 WHERE code = 'somecode') WHERE code = 'somecode';