查找和更改表中的值
问题描述:
I have a table with images in SQL database. It looks something like this.
SELECT * FROM eshop_images WHERE fg_idProduct='$id' AND main='1'
+----------+-----+------+--------------+
| idImages | url | main | fg_idProduct |
+----------+-----+------+--------------+
| 1 | x | 0 | 1 |
| 2 | x | 1 | 1 |
| 3 | x | 0 | 1 |
| 4 | x | 0 | 2 |
| 5 | x | 0 | 2 |
| 6 | x | 0 | 2 |
| 7 | x | 1 | 2 |
+----------+-----+------+--------------+
Each product can only have one main image, but what if I want to set some other image which is 0 as main one?
I have to go through the whole table where fg_idProduct=$id and SET them to null. I don't know how to do this, can somebody help me?
Thanks.
我在SQL数据库中有一个包含图像的表。 它看起来像这样。 p> \ n
SELECT * FROM eshop_images WHERE fg_idProduct ='$ id'AND main ='1'
+ ---------- + ----- + ------ + -------------- + \ N | idImages | 网址| 主要| fg_idProduct |
+ ---------- + ----- + ------ + -------------- +
| 1 | x | 0 | 1 |
| 2 | x | 1 | 1 |
| 3 | x | 0 | 1 |
| 4 | x | 0 | 2 |
| 5 | x | 0 | 2 |
| 6 | x | 0 | 2 |
| 7 | x | 1 | 2 |
+ ---------- + ----- + ------ + -------------- +
code>
每个产品只能有一个主图像,但如果我想设置一个0为主要图像的其他图像怎么办?
p>
我必须遍历整个表,其中fg_idProduct = $ id并将它们设置为null。 我不知道怎么做,有人可以帮帮我吗? p>
谢谢。 p>
div>
答
Try this:
UPDATE eshop_images
SET main = 0
WHERE main = 1
AND fg_idProduct = $id
答
UPDATE eshop_images
SET main = 0
WHERE main = 1
AND fg_idProduct = $id