mysql - Join between two tables based on multiple criteria -
i have table accounts
columns ip_from
, ip_to
, start_time
, end_time
, bytes
. there second table called all_audit
columns project
, ip
, time
. need join tables in order resulting table columns project
, time
, bytes
. things need considered time
matches records fall between start_time
, end_time
. ip
can match either ip_from
or ip_to
.
the schema 2 tables are:
accounts
+----------------+---------------------+------+-----+---------+-------+ | field | type | null | key | default | | +----------------+---------------------+------+-----+---------+-------+ | ip_from | char(15) | no | pri | null | | | ip_to | char(15) | no | pri | null | | | bytes | bigint(20) unsigned | no | | null | | | start_time | datetime | no | pri | null | | | end_time | datetime | yes | | null | | +----------------+---------------------+------+-----+---------+-------+
all_audit
+-----------+------------------+------+-----+---------+----------------+ | field | type | null | key | default | | +-----------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | no | pri | null | auto_increment | | project | varchar(255) | yes | | null | | | ip | varchar(32) | yes | mul | null | | | time | timestamp | yes | | null | | +-----------+------------------+------+-----+---------+----------------+
result
+-----------+------------------+------+-----+---------+----------------+ | field | type | null | key | default | | +-----------+------------------+------+-----+---------+----------------+ | project | varchar(255) | yes | | null | | | time | timestamp | yes | | null | | | bytes | bigint(20) unsigned| no | | null | | +-----------+------------------+------+-----+---------+----------------+
i know join don’t know start. pointers helpful not competent yet in sql statements willing learn.
i suspect looking this:
select aa.project , aa.time , a.bytes all_audit aa join accounts on (aa.ip = a.ip_from or aa.ip = a.ip_to) , aa.time between a.start_time , a.end_time
Comments
Post a Comment