我想根据 SELECT sql 查询更新多行。
我想在 SQL Shell 中完成所有操作!
这是我的选择:
SELECT @myid := id, @mytitle := title
FROM event
WHERE pid>0 GROUP BY title
ORDER BY start;
Run Code Online (Sandbox Code Playgroud)
然后,我想用这个伪代码做一个更新:
foreach($mytitle as $t)
BEGIN
UPDATE event
SET pid=$myid
WHERE title=$t;
END
Run Code Online (Sandbox Code Playgroud)
但我不知道如何在 SQL 中进行循环。
也许有一种方法可以在单个 sql 查询中实现?
我不想要任何 PHP !!!只有 SQL 外壳代码!!!
I want to update every rows with a pid with the id of the first occurence of an event. Start is a timestamp
我认为这应该做你想做的,但如果它没有(我不确定是否在 UPDATE 查询中加入子查询),那么你可以使用临时表。
UPDATE
event
JOIN (
SELECT
MIN(pid) AS minPID,
title
FROM
event
WHERE
pid > 0
GROUP BY
title
) AS findPIDsQuery ON event.title = findPIDsQuery.title
SET
event.pid = findPIDsQuery.minPID
Run Code Online (Sandbox Code Playgroud)