Subquery returns more than 1 row solution for update query using select statement

后端 未结 2 1258

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

相关标签:
2条回答
  • 2021-01-24 12:37

    You have to first think about what you want to do.

    Do you want to fetch one value and save it somewhere else? Then use SET value = (SELECT...). For this you need to make sure, the inner statement doesn't return more than one value.

    Or

    Do you need to be able to handle fetching multiple values? What do you want to do with these? Save all of them? All in one (use concat) or store them individually (one update per result)? Or select one of them? Maybe the first (LIMIT 1) or highest (MAX) or lowest (MIN) value?

    0 讨论(0)
  • 2021-01-24 13:03

    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)...
    

    or any one first value

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

    and so on...

    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 ....
    
    0 讨论(0)
提交回复
热议问题