I using sqlite and have a table that looks like:
I am trying to update refer column with values from col2 corresponding col1
I tried query like
update tab1
set refer = (select col2 from tab1 where col1 = refer)
where col1 = 2
This but is not working.
I have also tried
update tab1
set refer = (select tem1.col2
from tab1 tem1, tab1 tem2
where tem1.col1 = tem2.refer and tem2.col1=2)
where col1 = 2
This works.
But I am not sure whether this is the correct way to do.
Expected
Looking to your code seesm tha you need
update tab1
set refer = col2
when col1 = refer
and col1 = 2
that mean
update tab1
set refer = col2
when refer 2
oherwise fi yoru are looking for an update on the same table with subquery you should could use an inner join
in mysql
update tab1
INNER JOIN (
select col1, col2
from tab1
where col1 = refer ) t t.col1 = tabl1.col1 and col1 = 2
in sqllite you could use
update tab1
set refer = (select t.col2 from (
select col2 from tab1 where col1 = refer
) t )
where col1 = 2
Finally this query works perfectly:
update tab1
set refer = (select t1.col2 from tab1 as t1 where t1.col1 = tab1.refer)
where tab1.col1 = 2
Related
I've two tables like below (database MYSQL):
Table1
id
col1
Table2
id
col1 -> foreign key(Table1 - id)
col2
Now I want to insert value into Table2(col2) for all rows with the following condition:
Get value from Table1(col1) where Table2(col1) = Table1(id)
Example:
Before Insert:
Table1
id col1
1 value1
2 value2
Table2
id col1(fk) col2
3 1 NULL
4 2 NULL
After Insert:
Table2
id col1(fk) col2
3 1 value1
4 2 value2
I tried insert into with select join and where but apparently couldn't get it to work
insert into Table2(col2)
select t1.col1 from Table1 t1 join Table2 t2 on t1.id = t2.col1
Any pointers ?
Update
Got it working. Thanks for the pointers #rahul #frank I actually need to do update
update Table2 t2
set col2 = (SELECT t1.col1 FROM Table1 t1 where t1.id = t2.col1);
Update with JOIN
-- MySQL
UPDATE Table2
INNER JOIN Table1
ON Table2.col1 = Table1.id
SET Table2.col2 = Table1.col1;
Please check from url https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=a28fec2da45aa634f2509ec9299c2bed
I have this query
SELECT *, min(id)
FROM mytable
WHERE col2='specval'
GROUP BY col3
that selects rows I need.
I would like to set
UPDATE mytable
set col4 = col3+1000
for the rows selected above respectively.
So, how to update that rows by setting col4 in that table as col4=col3+1000 only for each selected row respectively?
Thank you.
you can use a query like this:
UPDATE mytable
set col4 = col3+1000
WHERE id in (
SELECT * FROM (
SELECT min(id)
FROM mytable
WHERE col2='specval'
GROUP BY col3
) as myids
);
You can use UPDATE with JOIN:
UPDATE mytable t1
INNER JOIN
(SELECT
col3, MIN(id) id
FROM
mytable
GROUP BY col3) t2 ON t1.col3 = t2.col3 AND t1.id = t2.id
SET
t1.col4 = t1.col3 + 1000;
You can use WHERE in update clause too. So depending on what "selected" means to you ( I'm guessing from your SELECT ), you might do something like this:
UPDATE mytable
SET col4 = col3+1000
WHERE col2 = 'specval';
In a case where I have to perform 2 queries, I wanted to replace it by simply one single line of query. Example:
select col1, col2 from tableA where col3 = 'a';
This will return (consider) 2 rows:
col1 col2
abc abc.bcd
xyz xyz.pqr
Now, in a second table we made a different query for each rows from query 1:
select col1 from tableB where col2 = 'abc';
(AND)
select col1 from tableB where col2 = 'xyz';
This will give a result set like:
TableB
col1
1111
2222
If the question is unclear kindly mention I shall try to elaborate with better example.
(although database vendor is not issue, I am comfortable with oracle or mysql. Thanks).
You basically just need a join between the two tables like this:
SELECT b.col1, a.col1
FROM tablea a
INNER JOIN tableb b ON a.col1 = b.col2
WHERE a.col3 = 'a'
Try this:
select col1
from tableB
where col2 in (select col1
from tableA
where col3='a')
You can use the single query as,
select col1, col2 from tableB where col2 = 'abc' or col2='xyz'
It' s not completely clear to me, but I think you are looking for a usual Join expression?
SELECT B.Col1, A.Col1 FROM TableA A inner join TableB B on A.Col1 = B.Col2
Please refer to the Join expression. This example is for SQL Server and newer versions of Oracle.
You can make a single query like :
select col1 from tableB where col2 = 'abc' or col2 = 'xyz';
And if you have large number of strings to check for then you can use :
select col1 from tableB where col2 in ('abc','xyz','mno');
Update: you can use nested queries like :
select col1 from tableB where col2 in (select col1 from tableA where col3='a');
But make sure that data type of col1 inside that nested query and data type of col2 after where match.
Couldn't find a solution yet... although it is probably a newbie question, I haven't been able to overcome... hope someone can give a hand.
I have a MySQL table that has:
Blockquote
Col1 Col2
Row1 A null
Row2 A A1
Row3 A null
Row4 B null
Row5 B B1
etc
> Blockquote
How do I construct an SQL update to update Col2, so that the values on Col2 replace the null, ie, C2R1 and C2R3 gets A1, and C2R4 gets B1 ?
You can calculate the required values as follows:
CREATE TEMPORARY TABLE yourtemptable AS
SELECT yourtable.col1, T1.col2
FROM yourtable
JOIN
(
SELECT col1, MAX(col2) AS col2
FROM yourtable
GROUP BY col1
) T1
ON yourtable.col1 = T1.col1
You can then either drop/truncate the original table and recreate it using these values, or if you can't drop the table you can instead perform a multi-table update.
Although it might not work (because the MySQL documentation states that Currently, you cannot update a table and select from the same table in a subquery.), you should try with something like:
UPDATE table1 SET col2 = (SELECT t2.col1 FROM table1 t2 WHERE t2.col1 = table1.col1 AND NOT t2.col2 IS NULL LIMIT 1) WHERE table1.col2 IS NULL
I have two tables a source and a destination. I want to flag the destination table to be updated if any of the corresponding columns in source columns are different. The columns can be null.
Currently this seems very clumsy:
UPDATE destination d
JOIN source s
ON d.id = s.id
SET d.updateFlag = 1
WHERE ( (d.col1 IS NULL AND s.col1 IS NOT NULL)
OR (d.col1 IS NOT NULL AND s.col1 IS NULL)
OR (d.col1 <> s.col1)
)
OR
( (d.col2 IS NULL AND s.col2 IS NOT NULL)
OR (d.col2 IS NOT NULL AND s.col2 IS NULL)
OR (d.col2 <> s.col2)
)
...etc...
OR
( (d.colN IS NULL AND s.colN IS NOT NULL)
OR (d.colN IS NOT NULL AND s.colN IS NULL)
OR (d.colN <> s.colN)
)
Ideally I could do something like:
UPDATE destination d
JOIN source s
ON d.id = s.id
SET d.updateFlag = 1
WHERE HASH(d.col1,d.col2,...etc...,d.colN) <> HASH(s.col1,s.col2,...etc...,s.colN)
Some additional info. The columns are all different data types (some ints, some bits, some strings) and we are using a flavor of MySQL 5.1.
You can simplify your statement by using the NULL-safe equal to operator:
UPDATE destination d
JOIN source s
ON d.id = s.id
SET d.updateFlag = 1
WHERE !(d.col1 <=> s.col1)
OR !(d.col2 <=> s.col2)
...etc
As an alternative, you can use a UNION to find duplicate rows:
SELECT tbl, id, col1, col2, col3
FROM (
SELECT 't1' AS tbl, id, col1, col2, col3
FROM t1
UNION ALL
SELECT 't2' AS tbl, id, col1, col2, col3
FROM t2
) tmp
GROUP BY id, col1, col2, col3 HAVING COUNT(*) = 1;
You can then use the result from that in your update query:
UPDATE destination d
SET d.updateFlag = 1
WHERE EXISTS (
SELECT NULL FROM (
SELECT id, col1, col2, col3 FROM t1
UNION All
SELECT id, col1, col2, col3 FROM t2
) tmp
WHERE d.id = tmp.id
GROUP BY id, col1, col2, col3
HAVING COUNT(*) = 1
)
You might want to check out mk-table-checksum, which is one of the tools in Maatkit. This does what you're describing, calculating a checksum for the entire row and comparing it to the corresponding row in another database.
Many people use this tool to verify that a slave is a true replica of its master, but you can compare any two databases that have similar metadata.