I have a table that will be updated using records from another table, and I do this to output information from one system (database) to another. The script is a bit complicated, but I desperately need help:-s
There are 3 tables component, scanandstage_link
component
component_id stage_id
------------ --------
1 NULL
2 NULL
3 NULL
4 NULL
5 NULL
scan
scan_id component_id scanner_id date_scanned
------- ------------ ---------- -----------------------
1 1 scanner_a 2012-01-01 07:25:15.125
2 1 scanner_b 2012-01-02 08:14:05.456
3 2 scanner_a 2012-01-01 12:05:45.465
4 3 scanner_a 2012-01-01 19:45:12.536
5 1 scanner_c 2012-01-03 23:33:54.243
6 2 scanner_b 2012-01-02 11:59:12.545
stage_link
stage_link_id scanner_id stage_id
------- ---------- ----------
1 scanner_a 1
2 scanner_b 1
3 scanner_c 2
4 scanner_d 2
5 scanner_e 2
6 scanner_f 3
I need a updatetable componentand a setfield stage_idaccording to the last view. Each scan transfers the component to the scene, depending on the scanner used. I wrote the following query for a updatetable component, but it gives an error message:
Subquery returned more than 1 value. This is not permitted when the subquery follows '='
Inquiry:
UPDATE component
SET stage_id = (select stage_id
from(
select scn.scanner_id, sl.stage_id
from scan scn
INNER JOIN stage_link sl ON scn.scanner_id = sl.scanner_id
where scn.date_scanned = ( select temp_a.max_date
from ( SELECT x.component_id, MAX(x.date_scanned) as max_date
FROM scan x
where component_id = x.component_id
GROUP BY x.component_id
) as temp_a
where component_id = temp_a.component_id)
) as temp_b
)
I am working on MS SQL Serverand want to sort it using PHPor any other language.
, . !
: -)