mysql#1242 - 子查询返回超过1行

时间:2016-11-22 20:16:21

标签: mysql

这是针对我的论文而且死路一条,后来我不知道我在这里做错了什么..我希望有人可以帮助我知道这里有什么问题谢谢

SELECT 
        flower_id,
        flower_name,
        flower_description,
        flower_price,
        flower_category,
        (quantity - (SELECT 
                SUM(q.quantity_value)
            FROM
                orders_details od
                    INNER JOIN
                cart_details cd ON cd.cart_id = od.cart_id
                    INNER JOIN
                quantities q ON q.quantity_id = cd.quantity_id
            WHERE
                od.flag = 1 AND cd.flower_id = flower_id
            GROUP BY cd.flower_id)) AS 'quantity',
        mfg_date,
        exp_date
    FROM
        flower_details,
        categories
    WHERE
        flower_details.flower_category = categories.category_id

我在这里做的是从客户购买的产品总数减去库存产品

1 个答案:

答案 0 :(得分:0)

如果您的子选择返回多行,则应使用subselect上的内连接加入总和 如果您的子选择返回的行数多于行,则应使用子选择内部联接的内部联接加入总和

      SELECT 
        flower_details.flower_id,
        flower_name,
        flower_description,
        flower_price,
        flower_category,
        flower_details.quantity - t1.quantity, 
        mfg_date,
        exp_date
    FROM flower_details 
    INNER JOIN categories ON flower_details.flower_category = categories.category_id
    INNER JOIN       (
                SELECT  cd.flower_id , 
                SUM(q.quantity_value) AS quantity
            FROM
                orders_details od
                    INNER JOIN
                cart_details cd ON cd.cart_id = od.cart_id
                    INNER JOIN
                quantities q ON q.quantity_id = cd.quantity_id
            WHERE
                od.flag = 1 AND cd.flower_id = flower_id
            GROUP BY cd.flower_id
    )  t1 on flower_details.flower_id = t1.flower_id