普通网友 2018-05-01 14:17
浏览 125

我怎样才能在mysql中纠正以下错误(子查询返回超过1行)

I have five tables such as tb_rawmaterial,tb_ingredients,tb_food, tb_order_items, tb_stock.

tb_rawmaterial has all the rawmaterial for a food

tb_ingredients connects the tb_food and the tb_rawmaterial with food_id as foreign key

tb_orderitems connects the tb_foods with food_id as foreign key.

tb_stock has all the rawmaterial stock level details it connects tb_rawmaterial with material_id as foreign key.

now what i have to do is :

i have to write an update query to change the stock level when make an order for a food, i wrote the below query:

UPDATE tb_stock set tb_stock.quantity = (tb_stock.quantity-(
    SELECT tb_ingredients.usage_volume * sum(tb_order_item.qty)
    FROM tb_ingredients 
    INNER JOIN tb_order_item 
    ON(tb_order_item.food_id=tb_ingredients.food_id
    AND tb_order_item.order_id='ORD-T01-00289') 
    GROUP BY tb_ingredients.food_id )) 
    WHERE material_id=(SELECT tb_ingredients.material_id
    FROM tb_ingredients 
INNER JOIN tb_order_item 
ON(tb_order_item.food_id=tb_ingredients.food_id
AND tb_order_item.order_id='ORD-T01-00289')
GROUP BY tb_ingredients.food_id)

BUT

it works if the raw material data is only one, if the same raw material data repeat again it gives the below error:

Subquery returns more than 1 row

How can i rectify this? Please advice me

  • 写回答

1条回答 默认 最新

  • doushijia5684 2018-05-01 14:18
    关注

    in order to limit the rows of the sub-queries results ...

    you can either add LIMIT 1 to the sub-query

    or select by SELECT DISTINCT/GROUP BY.

    using LEFT JOIN instead of INNER JOIN might also return less records.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?