1

您好,我想将一些行从我的pricelist表添加到products表中。我正在尝试products通过测试数据来填写我的表格。是否可以进行查询,我可以在我的products表中添加随机数量的行?主要问题是我在 select 语句中的行数比在 insert 语句中的行数多。

INSERT INTO products(product_name, product_price)
SELECT name_product, price_product, IF(RAND() > 0.2,1,0) AS random
FROM pricelist
HAVING random = 1
4

2 回答 2

4

将您的条件放在WHERE子句中。您的计算列random没有意义,因为您没有将其插入到另一个表中。

INSERT INTO products(product_name, product_price)
SELECT name_product, price_product
FROM pricelist
WHERE RAND() > 0.2 = 1
于 2013-09-27T13:43:28.507 回答
0

You mean you have more columns in select statement than in insert statement. The column number must match. There are also better ways to choose random rows...

INSERT INTO products(product_name, product_price)
SELECT name_product, price_product
FROM pricelist
ORDER BY RAND()
LIMIT 5
于 2013-09-27T14:08:15.943 回答