子查询使用select语句返回多于1行的更新查询解决方案

Subquery returns more than 1 row solution for update query using select statement(子查询使用select语句返回多于1行的更新查询解决方案)
本文介绍了子查询使用select语句返回多于1行的更新查询解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个查询,其中我使用 select 语句编写了更新语句.但不幸的是,得到错误子查询返回超过 1 行.我知道错误在哪里.但我不知道相同的解决方案.谢谢.这是查询:

Hello i have query in which i have written update statement using select statement. But unfortunately getting errors subquery returns more than 1 row. I know where the error is coming. But i dont know solution for the same.Thank you. Here is the query:

UPDATE adsetest.dashboard_widget_users
SET configuration=
  (SELECT DISTINCT ad_news_texte.headline
   FROM autodo.ad_news_texte
   INNER JOIN autodo.ad_news_oe
     ON ad_news_texte.news_id = ad_news_oe.id_ad_news
   INNER JOIN autodo.ad_news
     ON ad_news_oe.id_ad_news = ad_news.id
   WHERE ad_news.datum_archiv BETWEEN
     curdate() - INTERVAL DAYOFWEEK(curdate()) + 28 DAY AND curdate())
WHERE dsnr_yw_user = 1 AND dsnr_dashboard_widget = 1

推荐答案

当你使用 update 和 SET configuration=(SELECT ...) 子查询必须返回不超过一个值(一个排).如果它返回多个值,您如何将两行表分配给例如标量 configuration 字段.因此,您应该弄清楚为什么您的子查询返回多行并修复子查询或决定在多行的情况下选择哪个 ONE 值进行更新.例如你可以选择最大值

When you use update with SET configuration=(SELECT ...) the subquery has to return no more than one value (one row). If it returns more than one value how do you assign two rows table for example to scalar configuration field. So you should figure out WHY your subquery returns more than one row and fix the subquery or decide which ONE value to select for update in case of more than one row. For example you can select maximum value

SELECT MAX(ad_news_texte.headline)...

或任何一个第一个值

(SELECT ad_news_texte.headline)... LIMIT 1)

等等……

如果您需要连接所有行并将其放入一行 configureation 您可以使用 GROUP_CONCAT() mysql函数:

If you need to concatenate all rows and put it into one row configureation you can use GROUP_CONCAT() mysql function:

SET configuration=(SELECT GROUP_CONCAT(DISTINCT ad_news_texte.headline) FROM ....

这篇关于子查询使用select语句返回多于1行的更新查询解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)