[英]Insert data to table where select statement has more columns
Hello i would like to add some rows from my pricelist
table to products
table. 您好我想从我的
pricelist
表中添加一些行到products
表。 I am trying to fill my products
table by testing data. 我试图通过测试数据来填充我的
products
表。 Is possible to make query where i can add random amount of rows to my products
table? 是否可以在我的
products
表中添加随机行数的查询? The main problem is that i have more rows in select statement than in insert statement. 主要问题是我在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
Put your condition in the WHERE
clause. 将您的条件放在
WHERE
子句中。 Your calculated column random
does not makes sense since you don't to insert it on the other table. 您计算的列
random
没有意义,因为您不要将其插入到另一个表中。
INSERT INTO products(product_name, product_price)
SELECT name_product, price_product
FROM pricelist
WHERE RAND() > 0.2 = 1
You mean you have more columns in select statement than in insert statement. 你的意思是select语句中的列比insert语句中的列多。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.