0

I apologise now for the wording of the question, wasn't quite sure how to articulate this.

I have the following MySQL query:

select mn.meter_name, m.meter_number, m.meter_location, r.rates_meter_rate, r.rates_meter_PPD, m.meter_ID, mt.metertracking_periodend, mt.metertracking_read, mt.metertracking_readend FROM db_meters m, db_meters_name mn, db_rates_meter r, db_meters_tracking mt WHERE m.ICP_ID = '227' AND m.meter_name_ID = mn.meter_name_ID AND r.meter_ID = m.meter_ID AND (r.rates_meter_start <= '2013-08-31' AND r.rates_meter_end >= '2013-08-31') AND mt.meter_ID = m.meter_ID ORDER BY ABS( DATEDIFF( metertracking_periodend, NOW() ) ) 

It then returns:

**meter_name meter_number   meter_location   rates_meter_rate rates_meter_PPD meter_ID metertracking_periodend metertracking_read metertracking_readend**
 Anytime      9011656:1     in front office       0.15560          10.00         15            2013-07-31              685               69665
 Anytime      9011656:2     in front office       0.09000          10.00         16            2013-07-31              293               1022
 Anytime      9011656:1     in front office       0.15560          10.00         15            2013-06-30              589               68980

I want to limit the result so that each meter only returns once, so in this case meters(ID) 15 & 16

I hope someone can help, and thanks in advance.

4

2 回答 2

0

Group by在您的声明中使用:

select mn.meter_name, m.meter_number, m.meter_location, r.rates_meter_rate, r.rates_meter_PPD, m.meter_ID, mt.metertracking_periodend, mt.metertracking_read, mt.metertracking_readend FROM db_meters m, db_meters_name mn, db_rates_meter r, db_meters_tracking mt WHERE m.ICP_ID = '227' AND m.meter_name_ID = mn.meter_name_ID AND r.meter_ID = m.meter_ID AND (r.rates_meter_start <= '2013-08-31' AND r.rates_meter_end >= '2013-08-31') AND mt.meter_ID = m.meter_ID group by meter_ID ORDER BY ABS( DATEDIFF( metertracking_periodend, NOW() ) ) 
于 2013-08-29T23:33:32.807 回答
-1

好吧补充:

GROUP BY meter_ID

应该做的工作。

我不确定您的其他字段代表什么,以及您是否想在查询中使用某种聚合函数,例如 SUM()。

于 2013-08-29T23:35:12.210 回答