如何通过选择查询更新表中的列?

时间:2019-04-24 17:58:40

标签: sql phpmyadmin

我有一个查询,但是我想将查询结果更新到第三,第四,第五和第六列中的另一个表,因为我已经在第一和第二列中有值了。

我的查询:

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;

1364-字段“客户名称”没有默认值

1 个答案:

答案 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;