![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I'm working for an energy meter company. The energy meters reports consumption to a MySQL database. We are now in the works of developing virtual meters in wich our customers can create formulas of real meters. Every energy meter has it's own table with three columns; consumption, meter_reading and date_time. SELECT (COALESCE(t2.consumption, 0)+COALESCE(t1.consumption, 0)) AS cons, t2.date_time, t1.date_time FROM t2 LEFT JOIN t1 ON t1.date_time = t2.date_time WHERE (t2.date_time BETWEEN '2010-09-01 00:00:00' AND '2010-09-30 23:59:59') OR (t1.date_time BETWEEN '2010-09-01 00:00:00' AND '2010-09-30 23:59:59') if there are data in t1 wich is not in t2, the query will not match it, how can I JOIN both (or more) ways? |
#3
| |||
| |||
|
|
Il 01/10/2010 14.30, Johnny Beeper ha scritto: I'm working for an energy meter company. The energy meters reports consumption to a MySQL database. We are now in the works of developing virtual meters in wich our customers can create formulas of real meters. Every energy meter has it's own table with three columns; consumption, meter_reading and date_time. SELECT (COALESCE(t2.consumption, 0)+COALESCE(t1.consumption, 0)) AS cons, t2.date_time, t1.date_time *FROM t2 *LEFT JOIN t1 ON t1.date_time = t2.date_time WHERE (t2.date_time BETWEEN '2010-09-01 00:00:00' AND '2010-09-30 23:59:59') OR (t1.date_time BETWEEN '2010-09-01 00:00:00' AND '2010-09-30 23:59:59') if there are data in t1 wich is not in t2, the query will not match it, how can I JOIN both (or more) ways? If you have a left join and add some where condition on the t2 table... the query won't properly work. You can try with a sub-query like SELECT ... FROM t1 left join ( * * * * select ... from t2 where ... ) s on t1.date_time=s.date_time where t1 ... M. |
#4
| |||
| |||
|
|
The problem is not with the WHERE clause, even if I remove it, it won't fetch all rows. |
#5
| |||
| |||
|
|
Il 01/10/2010 16.24, Johnny Beeper ha scritto: The problem is not with the WHERE clause, even if I remove it, it won't fetch all rows. Dumb question: are you sure t1 contains rows that follow the where condition ? M. |
#6
| |||
| |||
|
|
I'm working for an energy meter company. The energy meters reports consumption to a MySQL database. We are now in the works of developing virtual meters in wich our customers can create formulas of real meters. Every energy meter has it's own table with three columns; consumption, meter_reading and date_time. SELECT (COALESCE(t2.consumption, 0)+COALESCE(t1.consumption, 0)) AS cons, t2.date_time, t1.date_time *FROM t2 *LEFT JOIN t1 ON t1.date_time = t2.date_time WHERE (t2.date_time BETWEEN '2010-09-01 00:00:00' AND '2010-09-30 23:59:59') OR (t1.date_time BETWEEN '2010-09-01 00:00:00' AND '2010-09-30 23:59:59') if there are data in t1 wich is not in t2, the query will not match it, how can I JOIN both (or more) ways? Best regards Johnny |
#7
| |||
| |||
|
|
I'm working for an energy meter company. The energy meters reports consumption to a MySQL database. We are now in the works of developing virtual meters in wich our customers can create formulas of real meters. Every energy meter has it's own table with three columns; consumption, meter_reading and date_time. SELECT (COALESCE(t2.consumption, 0)+COALESCE(t1.consumption, 0)) AS cons, t2.date_time, t1.date_time FROM t2 LEFT JOIN t1 ON t1.date_time = t2.date_time WHERE (t2.date_time BETWEEN '2010-09-01 00:00:00' AND '2010-09-30 23:59:59') OR (t1.date_time BETWEEN '2010-09-01 00:00:00' AND '2010-09-30 23:59:59') if there are data in t1 wich is not in t2, the query will not match it, how can I JOIN both (or more) ways? |
![]() |
| Thread Tools | |
| Display Modes | |
| |