mysql求sql语句 查询订单明细金额累加与订单总金额不等的订单

[复制链接]
查看11 | 回复3 | 2011-2-17 01:10:41 | 显示全部楼层 |阅读模式
两张表一张订单表order 里边有总金额subtotal
一张订单明细表order_details 里边按不同产品有金额cost
两张表通过order_id关联现有很多订单
要求查询将订单明细金额累加后于订单总金额不等的订单找出来返回所有不等的订单order_id

求高手解答
回复

使用道具 举报

千问 | 2011-2-17 01:10:41 | 显示全部楼层
<pre id=\\\"best-answer-content\\\">select *
from `order`r inner join
(selectorder_id ,sum(cost) as c from order_details ) as x
on r.order_id = x.order_id
where r.subtotalx.c

select * from \\\'order\\\' r where r.subtotal(select sum(cost) from order_details d where d.order_id =r.order_id)
回复

使用道具 举报

千问 | 2011-2-17 01:10:41 | 显示全部楼层
<p>

<pre class=\\\"answer-content\\\">SELECT
o1.order_id,
MAX(o1.subtotal)AS订单总金额,
SUM(o2.cost) AS订单明细合计
FROM
`order` o1 JOIN order_detailso2ON(o1.order_id = o2.order_id)
GROUP BY
o1.order_id
HAVING
MAX(o1.subtotal) SUM(o2.cost)

</p>
回复

使用道具 举报

千问 | 2011-2-17 01:10:41 | 显示全部楼层
<p>

<pre class=\\\"answer-content\\\">select order_id from order as o left join order_details as d on o.order_id = d,order_id where o.subtotal = d.cost

</p>
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行