I am using Mysqli
How can I do a single query that filters a subquery result? Is it possible?
I need to find users from a subquery result in a particular timestamp period.
SELECT * FROM Table WHERE (using subquery results i.e SELECT ...) BETWEEN 2017-01-08 09:00:00 AND 2017-01-08 12:00:00.
I should get back Mr A and Mr B.
Subquery results:
TimeIn User TimeOut
2017-01-06 10:00:00 Mr A 2017-01-08 11:00:00
2017-01-07 10:00:00 Mr A 2017-01-08 12:00:00
2017-01-08 10:00:00 Mr A 2017-01-08 13:00:00
2017-01-08 09:00:00 Mr B 2017-01-08 11:00:00
Thank you for your kind help as I am quite lost on how to do this.
Like this
SELECT * FROM
(
select ...
) subquery_alias_name
where timeIn BETWEEN '2017-01-08 09:00:00' AND '2017-01-08 12:00:00'
Don't forget to give the subquery an alias name
Related
If I have the following table,
Name
Date
Liam
2020-05-05 12:00:00
John
2020-12-15 03:45:00
Ross
2021-03-20 02:00:00
Ross
2021-03-20 02:05:00
Ross
2021-03-20 04:00:00
Ross
2021-03-20 04:30:00
Ross
2021-03-20 03:00:00
Jane
2021-03-20 02:00:00
Assume that NOW() gives 2021-03-20 10:00:00.
I want to remove all rows other than first three(based on time) if the same person has more than 3 entries in the last 24 hours from right now.
So this table becomes,
Name
Date
Liam
2020-05-05 12:00:00
John
2020-12-15 03:45:00
Ross
2021-03-20 02:00:00
Ross
2021-03-20 02:05:00
Ross
2021-03-20 03:00:00
Jane
2021-03-20 02:00:00
I assume that table name is tbl_dates.
So delete every rows which exists in today except the last rows after the first 3 rows (Nested select).
delete from tbl_dates as d1 where not exists (
select tbl_tmp.`Date` from (
select d2.`Date` from tbl_dates as d2 where d2.`Name`=d1.`Name` and date(d2.`Date`)=current_date() order by d2.`date` asc limit 0,3
) as tbl_tmp where tbl_tmp.`Date`=d1.`Date`
) and date(d1.`Date`)=current_date()
tbl_tmp: This subquery acted as a table for the first 3 rows of a person in today.
not exists (select ...): This subquery acted as a condition to equal current delete statement row with the first 3 rows of a person in today.
Updated:
if You want to select in this condition you can use this:
select * from tbl_dates as d1 where exists (
select tbl_tmp.`Date` from (
select d2.`Date` from tbl_dates as d2 where d2.`Name`=d1.`Name` and date(d2.`Date`)=current_date() order by d2.`date` asc limit 0,3
) as tbl_tmp where tbl_tmp.`Date`=d1.`Date`
) or date(d1.`Date`)<>current_date()
MySql version which I Tested: 8.0.18
I have this table
FLIGHTS
FNO Departs Arrives Price
111 10:00:00 11:30:00 5000
222 13:30:00 18:00:00 6000
333 20:00:00 22:30:00 3000
444 22:45:00 23:30:00 1000
Requirement:
I want to calculate the TOTAL_TIME of Travel using only flights 111,222,333.
I tried TIMEDIFF and ADDTIME, but I am not able to add results of subquery and get in TIME format.
Try this:
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(Arrives, Departs)))) Total_Duration
FROM FLIGHTS
WHERE FNO IN (111,222,333);
See DEMO on SQL Fiddle.
In Name Out
2017-01-01 10:00:00 Sam 2017-01-01 17:00:00
2017-01-18 10:00:00 James 2017-01-18 12:00:00
2017-01-18 10:00:00 John NULL -> means still in office
How do I find all people who is in office at 2017-01-18 11:00:00 only for example?
SELECT * FROM Table WHERE '2017-01-08 11:00:00' BETWEEN IN AND OUT
-> This only returns James. (should be James and John)
SELECT * FROM Table WHERE '2017-01-08 11:00:00' BETWEEN IN AND NOW()
-> This returns Sam, James, John (should be James and John only)
Anyone can help? Somehow a I need to replace NULL (Out) Column with current timestamp. But using NOW() causes a problem for the BETWEEN statement.
Try this:
SELECT *
FROM myTable
WHERE '2017-01-18 11:00:00' BETWEEN `IN` AND COALESCE(`OUT`, NOW())
The query uses COALESCE to replace a NULL value in column OUT with the current date/time value.
Demo here
How to order date, like this - 2012-02-01 00:00:00 by the hour,minutes,and the seconds, not by the year/moth/day.
If i have..
2012-02-01 02:00:00
2012-03-01 20:00:00
2012-04-01 12:00:00
2012-05-01 07:00:00
I wan't to get this output.
Column tipe is timestamp.
2012-02-01 02:00:00
2012-05-01 07:00:00
2012-04-01 12:00:00
2012-03-01 20:00:00
ORDER BY TIME(date_column)
This will, however, slow down your queries, as it isn't possible to index the on-the-fly calculation. If you have a lot of records, or if this query runs frequently, you should break the time portion of the date into its own column so you can index it for faster sorting.
Use the TIME() function to extract the time portion of the expression passed, e.g.
mysql> SELECT TIME('2012-02-01 02:00:00');
-> '02:00:00'
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_time
I've a user table (MySQL) with the following data
id email creation_date
1 bob#mail.com 2011-08-01 09:00:00
2 bob#mail.com 2011-06-24 02:00:00
3 john#mail.com 2011-02-01 04:00:00
4 john#mail.com 2011-08-05 20:30:00
5 john#mail.com 2011-08-05 23:00:00
6 jill#mail.com 2011-08-01 00:00:00
As you can see we allow email duplicates so its possible to register several accounts with the same email address.
Now I need to select all adresses ordered by the creation_date but no duplicates. This is easy (i think)
SELECT * FROM (SELECT * FROM users ORDER BY creation_date) AS X GROUP BY email
Expected result:
id email creation_date
2 bob#mail.com 2011-06-24 02:00:00
6 jill#mail.com 2011-08-01 00:00:00
3 john#mail.com 2011-02-01 04:00:00
But then I also need to select all other adresses, ie. all that are not present in the result from the first query. Duplicate are allowed here.
Expected result:
id email creation_date
1 bob#mail.com 2011-08-01 09:00:00
4 john#mail.com 2011-08-05 20:30:00
5 john#mail.com 2011-08-05 23:00:00
Any ideas? Perfomance is important because the real database is very huge
SELECT * FROM a
FROM users a
LEFT JOIN (SELECT email, MIN(creation_date) as min_date GROUP BY email)x ON
(x.email = a.email AND x.min_date=a.creation_date)
WHERE x.email IS NULL
In SQL server we would do a Select statement using a rank.
Here are some MYSQL samples:
How to perform grouped ranking in MySQL
http://thinkdiff.net/mysql/how-to-get-rank-using-mysql-query/
I hope this helps.