---
title: "mysql嵌套子查询"
author: "msnvip"
date: "2008-03-28T11:49:42Z"
tags: []
slug: "mysql"
draft: false
lastmod: "2008-03-28T11:49:42Z"
id: 177187
url: "https://msnvip.iteye.com/blog/177187"
---

查询产生费用的车辆
应收，实收 的保险费用

```sql

select truck.ch ,
 (select sum(bxfee) from fee where truck_id = truck.id and `type`='付' ) fbxf,
 (select sum(bxfee) from fee where truck_id = truck.id and `type`='收' ) sbxf
from truck where id in (select distinct truck_id from fee)

```


查询本月产生费用的车辆
应收，实收 的保险费用


```sql

select truck.ch ,
 (select sum(bxfee) from fee where truck_id = truck.id and `type`='付' ) fbxf,
 (select sum(bxfee) from fee where truck_id = truck.id and `type`='收' ) sbxf
from truck where id in (select distinct truck_id from fee 
                       where month(`date`) = ?  )

```