如何避免子查询或如何节省时间

时间:2016-12-09 07:42:35

标签: php mysql loops sum subquery

您好我编辑了我的问题

有4个表

id | teamname |

1 | lal

2 | sobuj

构件

id |成员名称|团队|

1 | SAGOR | 1 |

2 |守门| 1 |

DPS

id |成员|团队|存款|退款| ddate

1 | 1 | 1 | 100 | 12 | 2016-09-01 |

2 | 2 | 1 | 120 | 34 | 2016-09-01 |

3 | 1 | 1 | 130 | 22 | 2016-09-01 |

4 | 3 | 1 | 120 | 0 | 2016-09-01 |

MVS

id |成员|团队| mvs_total |退款| ddate

1 | 1 | 1 | 100 | 12 | 2016-09-01 |

2 | 2 | 1 | 120 | 34 | 2016-09-01 |

3 | 1 | 1 | 130 | 22 | 2016-09-01 |

4 | 3 | 1 | 120 | 0 | 2016-09-01 |

这是我的查询

$result=mysql_query(select * from member where team='1');
$row=mysql_fetch_array($result);
do{

$aresult=mysql_query(select sum(deposit)-sum(refund)as balance from dps where member='$row[id]');
$arow=mysql_fetch_array($aresult);
echo $arow[balance];

$bresult=mysql_query(select sum(mvs_total)as mvs_balance from mvs where member='$row[id]' and date1<='2016-09-01');
$brow=mysql_fetch_array($bresult);

}while($row=mysql_fetch_array($result));
它花了太多时间 如何避免子查询或如何节省时间 建议PLZ 提前致谢

1 个答案:

答案 0 :(得分:0)

试试这个,

select sum(deposit)-sum(refund) from dps JOIN member ON (dps. member = member.id) WHERE team='1';