我有一个查询,但是我想将查询结果更新到第三,第四,第五和第六列中的另一个表,因为我已经在第一和第二列中有值了。
我的查询:
INSERT INTO customer_info (reader_ID,tag_no,area,max_timestamp)
SELECT aisle_info.reader_ID,tag_no,
aisle_info.area,
TIMESTAMPDIFF(SECOND,MIN(timestamp),MAX(timestamp)) AS MAXTIME
FROM tag_logs
INNER JOIN aisle_info ON
tag_logs.reader_ID = aisle_info.reader_ID
WHERE tag_no = 515988190124;
答案 0 :(得分:0)
在TSQL中-在将值更新(添加)到表update
中的现有行时,应该写insert into select
语句而不是customer_info
。伪代码应类似于:
Update C
set C.reader_ID = A.reader_ID,
C.tag_no = A.tag_no,
C.area = A.area,
C.max_timestamp = TIMESTAMPDIFF(SECOND,MIN(T.timestamp),MAX(T.timestamp))
from customer_info C
join aisle_info A on A. tag_no = C. tag_no --Change here for correct join condition
join tag_logs T ON T.reader_ID = A.reader_ID
WHERE T.tag_no = 515988190124;