开发者

Database set cell to NULL

开发者 https://www.devze.com 2023-03-31 18:44 出处:网络
I have the following table structure: Inventory: userid - a - b - c - d - e - f - g - h - i - j - k The letters represent an inventory\'s slots. Here are stored products\' ids. These products\' dat

I have the following table structure:

Inventory: userid - a - b - c - d - e - f - g - h - i - j - k

The letters represent an inventory's slots. Here are stored products' ids. These products' data are also stored in a table called Products. The values in Inventory are linked to the id of the item in Products

A user wants to throw a product from his inventory, clicks a button and using ajax开发者_开发问答 the id of the item is sent to the server.

How can I find and set to NULL the cell containing that product if I have no information other information than the id?

P.S. I am using Symfony framework, if this makes the things easier.


Not the best solution, just an idea:

UPDATE Inventory SET a = NULL WHERE a = id AND userid=uid;
UPDATE Inventory SET b = NULL WHERE b = id AND userid=uid;
...
UPDATE Inventory SET k = NULL WHERE k = id AND userid=uid;

This one should work, but I cannot run it to test:

UPDATE Inventory 
SET 
 a = CASE WHEN a=id THEN NULL ELSE a END,
 b = CASE WHEN b=id THEN NULL ELSE b END,
 c = CASE WHEN c=id THEN NULL ELSE c END,
 d = CASE WHEN d=id THEN NULL ELSE d END,
 e = CASE WHEN e=id THEN NULL ELSE e END,
 f = CASE WHEN f=id THEN NULL ELSE f END,
 g = CASE WHEN g=id THEN NULL ELSE g END,
 h = CASE WHEN h=id THEN NULL ELSE h END,
 i = CASE WHEN i=id THEN NULL ELSE i END,
 j = CASE WHEN j=id THEN NULL ELSE j END,
 k = CASE WHEN k=id THEN NULL ELSE k END
WHERE userid=uid;
0

精彩评论

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