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
Related
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
)
If I run the following SQL statement:
SELECT * FROM `user-data` t1
LEFT JOIN `users` t2 ON t1.userID = t2.id
WHERE t2.id IS NULL
It gives me all the rows that don't have a matching row in the users table, (because the user has been deleted). But I cannot simply turn this into a DELETE statement:
DELETE FROM `user-data` t1
LEFT JOIN `users` t2 ON t1.userID = t2.id
WHERE t2.id IS NULL
As I get the following error:
#1064 - 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 't1
LEFT JOIN `users` t2 ON t1.userID = t2.id
WHERE t2.id IS NULL' at line 1
What can I do to delete all the rows in user-data that don't have an existing user in table users?
instead of using LEFT OUTER JOIN and IS NULL try a WHERE NOT EXISTS. Something like this:
DELETE t1
FROM user-data t1
WHERE NOT EXISTS
(
SELECT id FROM FROM users WHERE user-data.userID = users.id)
)
A delete join query is absolutely valid in MySQL. I think the issue is that you did not specify an alias after DELETE. Hence, the following should work:
DELETE t1 FROM `user-data` t1
LEFT JOIN `users` t2
ON t1.userID = t2.id
WHERE t2.id IS NULL
I suspect the need for an alias here arises because it is not clear from which table we want MySQL to do a deletion. Note that using the WHERE NOT EXISTS approach does not require an alias after DELETE.
I seem to remember that you cannot use table aliases in a DELETE. Also you can specify the table where you want to delete from like this:
DELETE `user-data`.* FROM `user-data`
LEFT JOIN `users` ON `user-data`.userID = `users`.id
WHERE `users`.id IS NULL
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;
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';