Update table joined to another table with different conditions - sql-server-2008

I want to update one table which is inner join to another table, but the rows which should be updated should have specific condition or not exist in the second table:
Update T1
Set STATUS = 'R'
From table1 T1
inner join table2 T2
on T1.ID = T2.ID and T2.STATUS = 'F'
Update T1
Set STATUS = 'R'
From table1 T1
Where T1.ID not exists in(Select T2.ID from table2 T2)
How can I write it by one Query?

You can do an OR in the where clause. Like the following :
Update T1 Set STATUS = 'R' From table1 T1
inner join table2 T2 on T1.ID = T2.ID and T2.STATUS = 'F' or T1.ID not exists in(Select T2.ID from table2 T2)

Related

Using values from main query inside a subquery

I had a problem creating a MySQL query with a subquery.
I wanted to use some data from the main query on the subquery, as many times did.
But this time I wanted to use it in a JOIN and didn't worked. I really want to understand why this happens.
I will show you some examples that works and the one that didn't.
I made this simple structure to reproduce the example:
# table1
id field1
1 *first_value*
2 *another_value*
#table2
id field2
1 *second_value*
Using table1.id on the WHERE of the subquery to get a value, the most typical use for me (I know this can be a join, but i try to show the difference):
SELECT
t1.field1,
(
select t2.field2
FROM table2 as t2
WHERE t2.id = t1.id
) as field2
FROM table1 as t1
WHERE t1.id = '1';
You can use table1.id on the SELECT part too (not much sense in the example, but works):
SELECT
t1.field1,
(
select t1.id as field2
FROM table2 as t2
WHERE t2.id = t1.id
) as field2
FROM table1 as t1
WHERE t1.id = '1';
Now, if you try to use it on a JOIN inside the subquery, then, crashes:
SELECT
t1.field1,
(
select t1.id
FROM table2 as t2
LEFT JOIN table1 as t3 ON t3.id = t1.id
WHERE t2.id = t1.id
) as field2
FROM table1 as t1
WHERE t1.id = '1';
Kernel error: Error( 1054 ) 42S22: "Unknown column 't1.id' in 'on clause'"
Buuut, u can do the JOIN using the field in another subquery changing ON t3.id = t1.id to ON t3.id = (SELECT t1.id) ???
SELECT
t1.field1,
(
select t1.id
FROM table2 as t2
LEFT JOIN table1 as t3 ON t3.id = (SELECT t1.id)
WHERE t2.id = t1.id
) as field2
FROM table1 as t1
WHERE t1.id = '1'
I wonder to know why the third example query doesn't work while all others does.
Can someone explain this, please?
Thank you :)
That's because all elements in the ON clause of a JOIN, must belong the one of the joined tables, so as your t2.id must be equal to t1.id, you can do
SELECT
t1.field1,
(
select t1.id
FROM table2 as t2
LEFT JOIN table1 as t3 ON t3.id = t2.id
WHERE t2.id = t1.id
) as field2
FROM table1 as t1
WHERE t1.id = '1';

MySQL - Update values based on subquery

let's say I have select, which return me from table1:
ID Name
1 Bob
2 Alice
3 Joe
Then I want UPDATE values in another table based on this result:
UPDATE table2 SET Name = table1.Name WHERE ID = table1.ID
As I understood, I can only do internal select in one place, like:
UPDATE table2 SET Name = (select Name from table1) WHERE ...
And I don't know how to specify WHERE-condition.
all you should do is just join the tables like this.
UPDATE table2 t2
JOIN table1 t1 ON t1.id = t2.id
SET t2.name = t1.name;
RESULTS WITH JOIN
if you are set on doing it with a select you could do it like this.
UPDATE table2 t2,
( SELECT Name, id
FROM table1
) t1
SET t2.name = t1.name
WHERE t1.id = t2.id
RESULTS FROM SELECT
UPDATE table2
SET name = (SELECT table1.Name FROM table1 WHERE table1.id = table2.id)
WHERE apply_condition
EDIT:#1
UPDATE table2 t2, (SELECT id, name FROM table1) t1 SET t2.name = t1.name WHERE t1.id = t2.id
please read this link,another
Try this
Update table2
Set Name = (Select Name From table1 where table1.ID = table2.ID)
Where table2.ID In (Select ID From table1)

SQL trouble with COUNT

I have some SQL code that returns me some data from DB
SELECT t1.id as id, title, description FROM table1 t1
JOIN table2 t2 ON t1.id = t2.t1_id
WHERE t2.t3_id IN( SELECT id FROM table3 WHERE parent_id IN ( SELECT id FROM table3 WHERE parent_id = 1)) GROUP BY t1.id
I have some problem with counting number of rows of result. I know that I have to write almost the same code but with COUNT but I have there A problem, my code doesn't return me a number of rows.
Just use the COUNT(*) function. Also, your subqueries can be converted to a JOIN (and your sub-subquery is redundant):
SELECT COUNT(*)
FROM table1 t1
JOIN table2 t2
ON t1.id = t2.t1_id
JOIN table3 t2
ON t3.id = t2.t3_id
WHERE t3.parent_id = 1

How to write scripts with some logic between two tables in MySQL?

Here's my questions in writing some scripts in MySQL:
I get a table T1 with some columns called id, t1_col_01, t1_col_02, and a table T2 with some columns called id, t2_col_01, t2_col_02.
For each row R1 in T1, I want to update R1.t1_col_01 = 'Yes' if the there are multiple rows in T2 that has the same id column with R1.id. If not, set R1.t1_col_01 = 'No'.
I tried to write:
update T1, T2
set
T1.t1_col_01 = 'Yes'
where
(select count(*) from T2 where T2.id = T1.id) > 1
But it didn't work.
What you need is this:
update T1
inner join T2
on ( T2.id = T1.id )
set T1.t1_col_01 = 'Yes'
where (select count(*) from T2 where T2.id = T1.id) > 1
See it here on fiddle:
http://sqlfiddle.com/#!2/0edc4/1

Select in where clause, access to current parent select columns

I have a query which contains a select statement in it's where clause. My question is now, how can I access the parent's select's data.
Example:
select * from TABLE_1 as t1 INNER JOIN TABLE_2 as t2
where (... and ...) OR
(not exists(select * from TABLE_3 as t3
inner join TABLE_1 ON t3.t1_id = t1.id
The last line is where the error occurs: t1.id is not a column.
How can I access the current value from the table t1?
I'm using MySql 5.1
SELECT
*
FROM
TABLE_1 as t1
INNER JOIN TABLE_2 as t2 ON
t2.PK = t1.FK --Whatever your keys are
WHERE
(... and ...)
OR
(
NOT EXISTS (select * from TABLE_3 as t3 WHERE t3.t1_id = t1.id)
)
First of all, you need to declare what you will JOIN TABLE_2 on TABLE_1.
SELECT *
FROM TABLE_1 AS t1
INNER JOIN TABLE_2 AS t2 ON t2.t1_id = t1.id
t1.id = t2.t1_id is just an example, you will need to decide which columns you wish you join on. Then in your WHERE clause, you do not need to INNER JOIN on TABLE_1 again as you are already selecting from it.
SELECT *
FROM TABLE_1 AS t1
INNER JOIN TABLE_2 AS t2 ON t2.t1_id = t1.id
WHERE (... AND ...) OR
(
NOT EXISTS
(
SELECT *
FROM TABLE_3 AS t3
WHERE t3.t1_id = t1.id
)
)