[英]sql nested join inner and left join
Hi I want to filter logs using MySQL with a list of trackings. 嗨,我想使用带有跟踪列表的MySQL过滤日志。
Every log belongs to a server, 每个日志都属于一台服务器,
Every tracking belongs to a server and have 0..N patterns 每个跟踪都属于服务器,并且具有0..N个模式
Every pattern belongs to a tracking 每个模式都属于一个跟踪
I have 3 tables : 我有3张桌子:
logs : | id | ip | url | server_id | ...
tracking : | id | server_id | name | other fields...
pattern : | id | tracking_id | pattern |
I want to count logs that match tracking for a specific server my problem is that my query mix up tracking that have pattern and those that don't. 我想计算与特定服务器的跟踪匹配的日志,我的问题是我的查询混淆了有模式和无模式的跟踪。
SQL Fiddle : http://sqlfiddle.com/#!2/f11b1/2 SQL小提琴: http ://sqlfiddle.com/#!2/f11b1/2
SELECT COUNT(DISTINCT logs.ip), tr.name
FROM `logs`
INNER JOIN `trackings` as tr ON
( tr.server_id = logs.server_id )
AND -- OTHER conditions between log and tracking
LEFT JOIN `patterns` as pt ON
( pt.tracking_id = tr.id )
AND (logs.url LIKE pt.pattern )
GROUP BY tr.id
My problem is on the second join, if I use INNER JOIN patterns as pt ON
I get correct results but only on trackings that have some patterns, If I use LEFT JOIN patterns as pt ON
I get all tracking but with a false count (I get the result of SELECT COUNT(DISTINCT logs.ip) FROM logs
) 我的问题是在第二次
INNER JOIN patterns as pt ON
,如果我将INNER JOIN patterns as pt ON
我将获得正确的结果,但仅在具有某些模式的跟踪上才能获得结果;如果我将LEFT JOIN patterns as pt ON
我将获得所有跟踪,但计数错误( SELECT COUNT(DISTINCT logs.ip) FROM logs
获取SELECT COUNT(DISTINCT logs.ip) FROM logs
的结果
EDIT I Can get the correct result with a field in tracking that indicates if the tracking has patterns and a UNION : 编辑我可以在跟踪字段中获得正确的结果,该字段指示跟踪是否具有模式和UNION:
(
SELECT COUNT(DISTINCT lg.ip), tr.name
FROM `logs` as lg
INNER JOIN `trackings` as tr ON
( tr.server_id = lg.server_id )
AND (tr.hasPatterns = 1)
AND -- Other conditions
INNER JOIN `patterns` as pt ON
( pt.tracking_id = tr.id )
AND (lg.url LIKE pt.pattern )
WHERE
GROUP BY tr.id
)
UNION
(
SELECT COUNT(DISTINCT lg.ip), tr.name
FROM `logs` as lg
INNER JOIN `trackings` as tr ON
( tr.server_id = lg.server_id )
AND (tr.hasPatterns = 0)
WHERE
GROUP BY tr.id, lg.date
)
But I guess there is a way to do that without using Union... 但我想有一种方法可以不使用Union ...
You can put a conditional inside count
, so I think the following does what you want: 您可以添加一个有条件的内部
count
,因此我认为以下内容可以满足您的需求:
SELECT COUNT(DISTINCT (case when tr.hasPatterns = 1 and pt.tracking_id is not null
then lg.ip
when tr.hasPatterns = 0
then lg.ip
end)), tr.name
FROM `logs` as lg
INNER JOIN `trackings` as tr ON
( tr.server_id = lg.server_id )
AND -- Other conditions
LEFT JOIN `patterns` as pt ON
( pt.tracking_id = tr.id )
AND (lg.url LIKE pt.pattern )
WHERE
GROUP BY tr.id
EDIT: 编辑:
This is returning what you want: 这将返回您想要的:
SELECT COUNT(DISTINCT (case when tr.size = 0 and pt.tracking_id is not null
then lg.ip
when tr.size > 0 and lg.size > tr.size
then lg.ip
end)), tr.name
FROM `logs` as lg
INNER JOIN `trackings` as tr ON
( tr.server_id = lg.server_id )
LEFT JOIN `patterns` as pt ON
( pt.tracking_id = tr.id )
AND (lg.url LIKE pt.pattern )
GROUP BY tr.id;
Your SQL Fiddle has the additional condition lg.size > tr.size
which is not in the original question. 您的SQL Fiddle具有附加条件
lg.size > tr.size
,该条件lg.size > tr.size
原始问题中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.