I have an update query that when I run it instead of matching each unique row it is replicating the same row through the entire column.
Any help would be massively appreciated.
I have got
UPDATE Table1
SET Column1 = (SELECT Column1 FROM [Table2] WHERE Column2 = [Table2].Column2)
[Table2].Column2 referes to the column in Table2.
Column2 refers to the column in some table that has such a column. The innermost such table is Table2.
So this ends up being the same as Table2.Column2 = Table2.Column2.
To refer to the column in Table1, specify that table:
UPDATE Table1
SET Column1 = (SELECT Column1
FROM Table2
WHERE Table2.Column2 = Table1.Column2);
Related
I'm trying to update my table if any value from table1 = value from table2
Table1 as 1 column with data
Table2 as many columns with data
If table2(data) = table1(data) update
But isn't working
I had one code that was working if i set table2 with 1 column
This one is working but table2 needs to have 1 column only
UPDATE table1
SET column1 = 'correct'
WHERE column2 in (SELECT column1 from table2);
I want to be able to do having more column
maybe something like this:
UPDATE
SET column1 = 'correct'
WHERE column2 in (SELECT * from table2);
The error:
Result: sub-select returns 11 columns - expected 1
How should I do it?
Maybe you can do it with EXISTS:
UPDATE table1
SET column1 = 'correct'
WHERE EXISTS (
SELECT 1 FROM table2
WHERE table1.somecolumn = table2.someothercolumn
);
If you want to check table1.someothercolumn against multiple columns of table2:
UPDATE table1
SET column1 = 'correct'
WHERE EXISTS (
SELECT 1 FROM table2
WHERE table1.somecolumn in (table2.col1, table2.col2, ...)
);
Do you know how to create an Oracle query that will list my table name, followed by all the column names in that table? I have 5 tables in total.
Something like:
Table
Column1
Column2
Column3
Table2
Column1
Table3
Column1
Column2
I'm hard-pressed to imagine why you would want a single-column result that doesn't do anything to differentiate between what is a column name and what is a table name. You can do something like
select pseudo_column_name
from (
select table_name, table_name pseudo_column_name, 0 column_id
from user_tables
union all
select table_name, column_name, column_id
from user_tab_columns
)
order by table_name, column_id
I am trying to update one column in table1 from a column in table2. Here
is what i am doing but i am getting an ORA error.
ORA-01427: single-row subquery returns more than one row.
update table1 a
set a.art_num = (
select b.art_num from table2 b
where a.comp_id = b.comp_id );
Thanks so much in advance!
This happens because your subquery returns more than one result.
You should check this one:
select b.art_num
from table2 b
where a.comp_id = b.comp_id
You could try to select DISTINCT (search for distinct in the link for documentation) on the subquery:
update table1 a
set a.art_num = (
select distinct(b.art_num)
from table2 b
where a.comp_id = b.comp_id );
I am having two tables which i wish to join.
I am not using foreign keys.
table structure table1
id,val1
table structure table2
id,val2
I am using now following command:
update table1 set val1=(SELECT val2 FROM table2 WHERE table1.id LIKE table2.id)
How can i force sqlite not to insert a value in table1 if an id does not exist in table2?
To use exact comparisons, use = instead of LIKE.
To update only those rows where a match is found, use a WHERE clause:
UPDATE Table1
SET val1 = (SELECT val2
FROM Table2
WHERE Table1.id = Table2.id)
WHERE id IN (SELECT id FROM Table2);
If you omit the WHERE clause, rows without a match get updated with a NULL value.
I'd like to do something like this:
Suppose I have two tables, myTable1 and myTable2. Suppose both of these tables have columns myColumn1 and myColumn2.
update
myTable1
set
myTable1.myFlagColumn = 1
where
myTable1.myColumn1, myTable1.myColumn2
in
(select myTable2.myColumn1, myTable2.myColumn2 from myTable2)
Essentially, I want to change a value in myTable1 if there are any rows where the two columns in myTable1 and myTable2 match.
Is this possible?
Yes, but you will use an EXISTS clause:
update
myTable1
set
myTable1.myFlagColumn = 1
where
EXISTS
(select * FROM myTable2 WHERE myTable2.myColumn1 = myTable1.myColumn1
AND myTable2.myColumn2 = myTable1.myColumn2)