且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何选择在sql中使用相同ID插入哪一行?

更新时间:2023-02-13 20:27:33

INSERT INTO t2 (ID, STATUS, TIME)
SELECT ID, STATUS, MIN(TIME) FROM t1 t1top
WHERE EXISTS(SELECT * FROM t1 WHERE ID=t1top.ID AND STATUS='Complete')
GROUP BY ID, STATUS
ORDER BY CAST(ID AS UNSIGNED) ASC, STATUS DESC

插入后,如果你想根据你的例子看到结果,你必须运行以下select:

After the insert is made, if you want to see the result according to your example, you have to run the following select:

SELECT ID, STATUS, TIME FROM table_1
ORDER BY CAST(ID AS UNSIGNED) ASC, STATUS DESC

完全正确,但是我不想那样看,它也需要以这种方式插入到第二个表中,有什么想法吗?

thats EXACTLY RIGHT, however I dont want to just SEE it that way, it needs to be inserted in the second table in that manner as well, any idea?