D
D
Dmitry Kuzmin2016-10-14 09:17:01
MySQL
Dmitry Kuzmin, 2016-10-14 09:17:01

How to compose Update according to Select data by condition from another table?

There is a table 1 from which we get a selection:

SELECT a.region,a.host 
FROM ftpup a
 , ( SELECT info,type,timestamp, max(date) as time FROM ftpup GROUP BY type ) b 
WHERE a.type = b.type AND a.date = b.time AND a.status <> '0' ORDER BY a.`date` DESC

Sample result:
region --- host
SB --- H1
BS --- H2
PT --- H2
There is table 2, with the structure:
host --- region --- status
H1 --- SB --- 1
H2 - -- PT --- 0
H2 --- BS --- 1
How to compose update for table 2 and set status='2' where host = host from first select (result from table 1) and region=region from first select ( result from table 1)?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Kuzmin, 2016-10-14
@Dimkaa

Yes, I already blinded a little earlier. You have the same logic.

UPDATE table2
INNER JOIN (SELECT a.region,a.host 
FROM ftpup a
 , ( SELECT info,type,timestamp, max(date) as time FROM ftpup GROUP BY type ) b 
WHERE a.type = b.type AND a.date = b.time AND a.status <> '0' ORDER BY a.`date` DESC) AS table1
ON (table2.hostname = table1.host AND table2.region = table1.region)
SET table2.status=2

I
idShura, 2016-10-14
@idShura

I'm not sure, but try this (try on test data):

UPDATE my_tab2 as t2 
  JOIN (SELECT a.region,
               a.host 
          FROM ftpup a, 
               (SELECT info, 
         	           type, 
         	           timestamp, 
         	           max(date) as time 
         	      FROM ftpup 
              GROUP BY type ) b 
        WHERE a.type = b.type 
          AND a.date = b.time 
          AND a.status <> '0' ) AS t1 ON t1.host = t2.host and t1.region = t2.host
   SET t2.status = '2'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question