Php sql语句中的求和数量
我有这个功能Php sql语句中的求和数量,php,mysql,sql,codeigniter,Php,Mysql,Sql,Codeigniter,我有这个功能 public function get_quantity($product_id,$event_start_date) { $qStr ="SELECT order_items.quantity FROM order_items WHERE product_id = $product_id AND
public function get_quantity($product_id,$event_start_date) {
$qStr ="SELECT
order_items.quantity
FROM
order_items
WHERE
product_id = $product_id
AND
(SELECT shows.event_end_date FROM
shows
INNER JOIN order_items ON
shows.order_id = order_items.order_id
WHERE
order_items.product_id = $product_id
) > $event_start_date";
$query = $this->db->query($qStr);
$result = $query->row_array();
return (!empty($result) ? $result['quantity']:0 );
}
子查询返回许多结果。因此我们有许多数量,我必须对所有数量求和。
请帮我找出正确的功能。
提前感谢。使用
join
和sum()
SELECT
SUM(i.quantity) total_quantity
FROM
order_items i
INNER JOIN shows s
ON s.order_id = i.order_id
WHERE s.event_end_date > $event_start_date
AND i.product_id = $product_id
如果子查询返回的行数超过1行,则查询不应工作。不要加入子查询,改为与外部订单_项相关。所有产品数量或总体数量子查询返回的数量超过1row@umair你的评论与我的答案无关,因为它有一个连接而不是子查询。在提交之前,你看过你的代码了吗?你怎么能说它没有子查询?@MusaddiqKhan你能告诉我它在哪里有子查询吗?它使用join作为参考参见@MKhalidJunaid如果你在查询中进行查询,它就像你在上面的查询中一样被称为子查询<代码>产品id=$product\U id和(选择shows.event\u end\u dat