请教一个MySQL问题,修改某表的某个字段的值为随机 (m, n) 之间的不同的值,我画了一个表格来描述问题
1个回答
2013-03-21
展开全部
CREATE TABLE db_goods (
id int primary key,
name varchar(20),
orders int
);
INSERT INTO db_goods
SELECT 1, 'Apple', 5 UNION ALL
SELECT 2, 'banana', 6 UNION ALL
SELECT 3, 'cherry', 7 UNION ALL
SELECT 4, 'grape', 8 UNION ALL
SELECT 5, 'orange', 9;
UPDATE
db_goods
SET
orders = FLOOR(1 + RAND() * 100);
mysql> select * from db_goods;
+----+--------+--------+
| id | name | orders |
+----+--------+--------+
| 1 | Apple | 54 |
| 2 | banana | 29 |
| 3 | cherry | 80 |
| 4 | grape | 14 |
| 5 | orange | 30 |
+----+--------+--------+
5 rows in set (0.00 sec)
id int primary key,
name varchar(20),
orders int
);
INSERT INTO db_goods
SELECT 1, 'Apple', 5 UNION ALL
SELECT 2, 'banana', 6 UNION ALL
SELECT 3, 'cherry', 7 UNION ALL
SELECT 4, 'grape', 8 UNION ALL
SELECT 5, 'orange', 9;
UPDATE
db_goods
SET
orders = FLOOR(1 + RAND() * 100);
mysql> select * from db_goods;
+----+--------+--------+
| id | name | orders |
+----+--------+--------+
| 1 | Apple | 54 |
| 2 | banana | 29 |
| 3 | cherry | 80 |
| 4 | grape | 14 |
| 5 | orange | 30 |
+----+--------+--------+
5 rows in set (0.00 sec)
追问
有可能orders会产生相同的值吧?
有没有能让orders不会相同,就是唯一性。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询