mysql - Complex Two Table Join -
trying result set has 1 row per sensorrule row sensorrule.sensorid exists in sensordata.sensorid date(sensor.messagedate) = curdate() , time(sensordata.messagedate) => sensorrule.fromtime , time(sensordata.messagedate) => sensorrule.totime.
resulting set less number of rows in sensorrule if there no rows in sensordata current date (messagedate) time of messagedate between fromtime , totime.
sensorrule +-------------+--------------+------------+-------+--------------+ | sensorid | fromtime | totime | msg | name | +-------------+--------------+------------+-------+--------------+ | 27460 | 5:00 | 11:00 | work | bob | | 27461 | 4:00 | 15:00 | not | susan | | 27460 | 7:30 | 12:00 | max | rick | +-------------+--------------+------------+-------+--------------| sensordata +---------+---------------------+---------------+----------+ | room | messagedate | transactionid | sensorid| +---------+---------------------+---------------+----------+ | bedroom | 2014-05-30 07:48:14 | 332243181 | 27460 | | bedroom | 2014-05-30 05:48:14 | 332243182 | 27464 | | bedroom | 2014-05-22 07:36:21 | 332243183 | 27460 | | bedroom | 2014-05-22 09:23:21 | 332656534 | 27464 | | bedroom | 2014-05-21 06:34:22 | 332243185 | 27461 | | bedroom | 2014-05-20 06:15:06 | 332243187 | 27460 | | bedroom | 2014-05-20 07:40:34 | 332243172 | 27465 | | bedroom | 2014-05-19 09:27:56 | 332243191 | 27461 | | bedroom | 2014-05-18 06:34:08 | 332243101 | 27460 | +---------+---------------------+---------------+----------+ would resulting set like:
sensorid msg name 24760 work bob 27461 not susan note: data set above doesn't yield above result
select distinct s.sensorid,s.msg,s.name sensorrule s,sensordata d s.sensorid=d.sensorid , time(d.messagedate)>s.fromtime , time(d.messagedate)<s.totime;
Comments
Post a Comment