Friday, 15 August 2014

mysql - Combine results of two different queries into a single query -


I have a MySQL table with login logs. Each entry contains user email, IP address, timestamp and login result (0 failed, 1 success) are included.

  + ------------ + ---- -------------- + ------- ----- + -------- + | IP | Email | Day-time Results | + ------------ + ------------------ + ------------ + ---- ---- + | 2130706433 | User1@domain.com | 14264 9 362 | 0 | | 2130706433 | User1@domain.com | 14264 9 363 | 1 | | 2130706433 | User1@domain.com | 1426498364 | 0 | | 1134706444 | User2@domain.com | 14264 9 365 | 0 | + ------------ + ------------------ + ------------ + ---- ---- +  

My goal is to remove the count of unsuccessful login from a given timestamp and create the last logout timestamp for the user1 @ domain .com . In this case I would like to receive (For simplicity, all entries are after the required time limit).

  + -------- + ------ --- --- + | Counting | Day-time + -------- + ------------ + | 3 | 1426498364 | + -------- + ------------ +  

So far, I have created two different queries to remove the results separately

  as datetime FROM accesslogs as SELECT SELECT MAX (datetime), from which select e-mail = `osier1@domain.com (select the result) select COUNT (result ) As counting (select date from accesslogs datetime & gt; 1426498360 and result = 0) ` 

Now I want to combine them to get results with a single query I am trying to I was thinking about using the statement JOIN , but I do not know any columns where questions can be joined, what can I do?

You can use conditional aggregation for it,

  Select the amount (when results = 0 and datetime> 1426498360 then 1 and 0 then) as 'calculation', max (in case if email = 'user1@domain.com' then date expiry) from access logs As datetime;  

Here is an example

  mysql> Choose * from the exam; + ------------ + ------------------ + ------------ + ---- ---- + | IP | Email | Day-time Results | + ------------ + ------------------ + ------------ + ---- ---- + | 2130706433 | User1@domain.com | 14264 9 362 | 0 | | 2130706433 | User1@domain.com | 14264 9 363 | 1 | | 2130706433 | User1@domain.com | 1426498364 | 0 | | 1134706444 | User2@domain.com | 14264 9 365 | 0 | + ------------ + ------------------ + ------------ + ---- ---- + 4 lines set (0.00 seconds) mysql & gt; Choose - & gt; Zodiac (-> Case -> When results = 0 and datetime> 1426498360 -> then 1 and 0 end - & gt;) as `calculation ', -> Datetime (& gt; Case - & gt; when email = 'user1@domain.com' then 'datetime - & gt;) as datetime - & gt; by examining; + ------- + ------------ + | Counting | Day-time + ------- + ------------ + | 3 | 1426498364 | + ------- + ------------ + 1 line set (0.00 seconds)  

No comments:

Post a Comment