简体   繁体   English

高级SQL查询(查询和比较中的查询)

[英]Advanced SQL Query (query in query and comparison)

:) :)

I'm right now scratching my head while I try to figure out one or two complex MySQL queries I'm in need of. 我现在正在摸索,尝试找出一个或两个我需要的复杂MySQL查询。

Table "timestamps" consists of columns: name, login_time, logout_time 表“时间戳”由以下几列组成:名称,login_time,logout_time

An explanation of what I'm trying to achieve is this: 我要达到的目的的解释是:

in short: search for name X. Find all that NEVER EVER have been online at the same time ever. 简而言之:搜索名称X。查找从未在同一时间在线的所有内容。 If so, if login_time is equal to the comparing posts logout_time OR the comparing posts login_time is equal to the searched name's logout_time, it gains a "point" for probability. 如果是这样,如果login_time等于比较项logout_time或比较项login_time等于搜索名称的logout_time,则它获得概率的“分”。

The query at first should fetch all rows with name X that the logout_time is NOT NULL. 首先,查询应获取所有名称为X的logout_time为NOT NULL的行。

Then using those results (will be multiple rows), compare all results to ALL other rows in the table where the logouttime is NOT NULL and compare the login_time and logout_time to never EVER been online at the same time. 然后使用这些结果(将是多行),将所有结果与表中logouttime不为NULL的所有其他行进行比较,并将login_time和logout_time从未同时联机。

With that result, (those that haven't been online at the same time EVER against the name first searched) finally check if anyone has the first searched name login_time is equal to the comparing posts logout_time OR the comparing posts login_time is equal to the searched name's logout_time. 有了这个结果,(那些从未在同一时间针对第一个搜索到的名称同时在线的人)最后检查是否有人第一个搜索到的名称login_time等于比较帖子logout_time或比较帖子login_time等于搜索的帖子名称的注销时间。

Right now I'm using many, many single queries in Java against the MySQL server doing all the checks. 现在,我在Java中针对MySQL服务器使用许多查询,以进行所有检查。 Here is the method for comparing online times I've used and confirmed working. 这是比较我已使用并确认工作的在线时间的方法。

p1In, P1Out is the searched name's login_time and logout_time p2In, P2Out is login_time and logout_time of the one it's currently comparing against (one that have logout_time set(NOT NULL)) p1In,P1Out是所搜索名称的login_time和logout_time p2In,P2Out是当前正在比较的名称的login_time和logout_time(设置了logout_time(NOT NULL)的那个)

        public boolean OnlineAtTheSameTime(long p1In, long p1Out, long p2In, long p2Out) {
    boolean result = false;
    result = ((p1In > p2In || p1Out < p2Out) && (p1Out < p2In || p1Out > p2Out) && (p1In < p2In || p1In > p2Out));
    return result;
}

after that I later just check 之后,我稍后检查

if (p1In == p2Out || p2In == p1Out)

//give p2 a "point" for probability //给p2一个概率的“点”

I will later use this information, using probability, find out who's who thanks to them logging in/out before/after each other while at the same time never ever been online at once. 稍后,我将通过概率使用此信息,找出谁是谁,这要归功于他们彼此先后登录/注销,而同时又从未一次联机。

Due to the "massive" amounts of unnecessary queries I'm making, I'm wondering if all this is possible with one or more queries that probably will save me lots of database load and sheer amount of queries. 由于我要进行“大量”不必要的查询,我想知道一个或多个查询是否可以实现所有这些目的,这可能会节省大量数据库负载和大量查询。

Thanks a lot and looking forward to see what you can come up with. 非常感谢,并期待看到您能想到的。 (if I could I would upload an image of a row and the table structure) (如果可以的话,我会上传一行和表格结构的图片)

  1. Find the ids of everyone who has EVER been online at the same time. 查找曾经同时在线的每个人的ID。

  2. Use that as a subquery in the following to find the NEVER EVERs: 在下面将其用作子查询来查找“从不”:

     FROM real_table LEFT JOIN ( SELECT id ... ) AS ever USING (id) WHERE ever.id IS NULL 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM