質問

I found several similar questions, but none that got me past my roadblock. (This one is close How to 'insert if not exists' in MySQL?)

I need to read all the IDs except one from one table and then update another table with them. The destination table has 2 columns. One of those columns must be that foreign id. I want the other column to be a number I hard code into the query.

like this:

letter_id drop_letter_id
10        99

What happens in the application is that if someone subscribes to letter 10, then it unsubscribes them from letter 99. I want 99 to be exclusive to those that are not subscribed to anything else so I need to find all the letter ids that are not 99 and add them to this drop table as

letter_id drop_letter_id
x        99
y        99

where x and y are other letter id numbers.

sort of like

SELECT letter_id FROM letters WHERE letter_is<>99

and then

INSERT IGNORE INTO letter_drops (x,99)...
役に立ちましたか?

解決

If I got correctly, it's:

INSERT IGNORE INTO letter_drops SELECT letter_id, 99 FROM letters WHERE letter_id!=99
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top