PostgreSQL:使用同一表上的 select 语句更新字段
我需要更新随机选择的样本表中的字段。 如果我运行此查询,它将正确返回所选的 57 行:
SELECT * FROM table WHERE item1 = 'XX' AND item2 = 'JJJ' ORDER BY random() LIMIT (57)
我现在要做的是应用该 SELCT 语句来用“YY”更新 item1,但仅限于随机选择的 57 行。 我尝试这样做:
UPDATE table
SET item1 = 'YY'
WHERE id_item = (SELECT id_item FROM table WHERE item1 = 'XX' AND item2 = 'JJJ'
ORDER BY random() LIMIT (57))
但它返回错误:“用作表达式的子查询返回的不止一行”。
如何更新表中的随机行数? 提前致谢
I need to update a field in a table on a sample randomly selected.
If I run this query it returns correctly the selected 57 rows:
SELECT * FROM table WHERE item1 = 'XX' AND item2 = 'JJJ' ORDER BY random() LIMIT (57)
What I want to do now is to apply that SELCT statement to UPDATE the item1 with 'YY', but only for the 57 randomly selected rows.
I try this:
UPDATE table
SET item1 = 'YY'
WHERE id_item = (SELECT id_item FROM table WHERE item1 = 'XX' AND item2 = 'JJJ'
ORDER BY random() LIMIT (57))
but it returns the error: "more than one row returned by a subquery used as an expression".
How can I update a table for a random number of rows?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将“=”运算符更改为 IN。
Change the "=" operator to be IN instead.
Chage = “在”。
您需要将其更改为:
Chage = to "in".
You need to change it to this:
对于 WHERE 子句,您可能需要使用 IN 运算符而不是 =。
You may need to use IN operator instead of = for WHERE clause.