开发者

MySQL update table to fill nulls from another table

I have a table codes with fields id, code, issuedto, issuedtime which is pre-filled with items in code but which has many rows with NULLs in issuedto, issuedtime. I have another table entrants which has fields id, status.

I want to set codes.issuedto = winners.id for each row of winners with status = 'won'.

The problem is I don't have anything to join the tables on -- so I end up with a cross join and that's not 开发者_如何转开发what I want at all. What I really want is an inner join -- but without anything to join on. Anyone have any ideas?

EDIT: if I were doing this outside of SQL (which I might have to do?) the pseudocode would look something like:

rows = query("SELECT id FROM winners WHERE status='won'");
foreach (rows as r) {
    query("UPDATE codes SET issusedto=" + r.id + ", issued=NOW() WHERE issuedto IS NULL LIMIT 1");
}


OP here. After some extensive searching and experimentation, I have come to the conclusion this cannot be done in SQL. I wrote a short script to do this work for me, similar to the pseudocode listed in the question. ACID compliance is achieved by transactions (check your DBMS for details if this is important).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜