Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL Query Between Multiple Dates in another table
    primarykey
    data
    text
    <p>Feel like this should be something easy that I'm missing. I have a table with multiple timestamps throughout a day, and another table with date ranges. I want to select from the first table whenever it's timestamp is in any of the date ranges in the other table, for a specific day... Anyone have any clue how that could work or if it's even possible?</p> <h2>Edit</h2> <pre><code>SELECT count(*) FROM Completed /* Returns 59 */ SELECT count(u.user_id) AS Total FROM Completed c LEFT JOIN users u ON c.user_id = u.user_id LEFT JOIN logins l ON c.user_id = l.user_id WHERE c.start BETWEEN l.signIn AND l.signOut AND c.users_id = 1400 GROUP BY c.type, c.user_id ORDER BY c.type, Total /* Returns 57 Total */ </code></pre> <p>So, my three tables are:</p> <ul> <li>User. user_id int, fullname varchar.</li> <li>Complete. user_id int, type varchar, start datetime</li> <li>Logins. user_id int, signIn datetime, signOut datetime</li> </ul> <p>Essentialy I have multiple rows in the complete table, and I want to know how much that the user did, versus how much they had an opportunity to do. "Having the opportunity" is simply being signed in, so I'm trying to get a breakdown of each users total worked, versus available for any work that came in while they were signed in. The thing is they'll have multiple sign in periods throughout one day. I thought I was close above, but no cigar.</p> <h2>Edit 2</h2> <p>Okay, here's what the tables might look like. In the example below I'd want to see that John had the opportunity to work 4 and worked 2 of them. I wouldn't want to count complete_id 4 because he wasn't at work yet. My ideal result would look something like:</p> <pre><code>name | type | available | worked | sign in | sign out Jack | SUV | 1 | 1 | 2011-04-08 12:30:00 | 2011-04-08 13:00:00 Jack | SUV | 3 | 0 | 2011-04-08 13:30:00 | 2011-04-08 14:00:00 // Had An Opportunity to work, but didn't work any Jack | SUV | 5 | 2 | 2011-04-08 14:30:00 | 2011-04-08 15:00:00 // Worked two </code></pre> <p>users table</p> <pre><code>user_id | fullname 1400 | Jack 1401 | John </code></pre> <p>complete table</p> <pre><code>complete_id | user_id | type | start 1 | 1400 | Car | 2011-04-08 18:36:22 2 | 1400 | Boat | 2011-04-08 12:31:42 3 | 1401 | SUV | 2011-04-08 11:14:12 4 | 1400 | Car | 2011-04-08 1:56:52 5 | 1401 | Car | 2011-04-08 16:11:23 </code></pre> <p>logins table</p> <pre><code>login_id | user_id | signIn | signOut 1 | 1400 | 2011-04-08 1:00:00 | 2011-04-08 21:00:00 1 | 1401 | 2011-04-08 11:00:00 | 2011-04-08 21:00:00 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload