insert into mytable values
('100', '2015-07-14', '2015-07-25'),
('200', '2015-07-28', '2015-07-30')
I need result like below when i search in the above table
example:
2015-07-13 to 2015-07-29 (not available)
2015-07-15 to 2015-07-22 (available)
I try like below query
select * from mytable where
valid_from <= '2015-07-29' and valid_to >= '2015-07-13'
But it showing results is available.
Here Date's 13, 26, & 27 is not in the table.
Try this code
select * from mytable where
(valid_to between '2015-07-29' and '2015-07-13')
and
(valid_from between '2015-07-29' and '2015-07-13');
and I suggest you to use date in UNIX time stamp (Epoch format).
Just add some more constraints to the query:
select * from mytable where
valid_to >= '2015-07-29' and valid_to <= '2015-07-13' and valid_from >= '2015-07-29' and valid_from <= '2015-07-13';
Related
I have two dates. 2019-01-01(fromDate) and 2019-01-10(toDate). And now I want to search only the 13:00 to 16:00 time of each date. May I ask if is it possible using query only? Any answer is much appreciated
SELECT *
FROM table
WHERE fromDate >= 2019-01-01 AND toDate <= 2019-01-10
You can try to use STR_TO_DATE function with the format and HOUR function.
SELECT *
FROM table
WHERE
fromDate >= STR_TO_DATE('2019-01-01', '%Y-%m-%d')
AND
toDate <= STR_TO_DATE('2019-01-10', '%Y-%m-%d')
AND
(HOUR(fromDate) BETWEEN 13 AND 16 OR HOUR(toDate) BETWEEN 13 AND 16)
Use separate conditions on the date and on the time:
SELECT *
FROM table
WHERE fromDate >= '2019-01-01' AND
toDate < '2019-01-11' and
time(fromdate) between '13:00:00' and '16:00:00' and
time(todate) between '13:00:00' and '16:00:00;
Or, if you don't want 16:00:00 exactly, you can use hour():
SELECT *
FROM table
WHERE fromDate >= '2019-01-01' AND
toDate < '2019-01-11' and
hour(fromdate) in (13, 14, 15) and
hour(todate) in (13, 14, 15)
I want to find out if there is a difference in the result between using the two following queries on the same table:
SELECT * FROM table WHERE DATE BETWEEN 01/01/2007 AND 30/9/2007
and
SELECT * FROM table WHERE DATE >= 01/01/2007 AND DATE <= 30/9/2007
Is there a difference between using BETWEEN and >= <=?
SELECT * FROM order_details WHERE order_date BETWEEN CAST('2014-02-01' AS DATE) AND CAST('2014-02-28' AS DATE);
This MySQL BETWEEN condition example would return all records from the order_details table where the order_date is between Feb 1, 2014 and Feb 28, 2014 (inclusive). It would be equivalent to the following SELECT statement:
SELECT * FROM order_details WHERE order_date >= CAST('2014-02-01' AS DATE) AND order_date <= CAST('2014-02-28' AS DATE);
Source
So the answer is NO, there is no difference
SELECT *
FROM workshop_planning_history
WHERE STATUS =1
OR STATUS =2
AND start_date =2015 -11 -29
OR end_date <=2016 -01 -10
OR end_date >=2016 -01 -10
I am running the above query and i am expecting values from Workshop_planning_history whose status is either 1 or 2 but i am getting results which have a status of 3 along with the rows having status 1 & 2.
What's wrong with my query please Help ...
You have to add some parenthesis to the predicate, because the precedence of operators is NOT AND OR, so your query is interpreted as:
SELECT *
FROM workshop_planning_history
WHERE STATUS = 1
OR (STATUS = 2 AND start_date = '2015-11-29')
OR end_date <= '2016-01-10'
OR end_date >= '2016-01-10'
Change to:
SELECT *
FROM workshop_planning_history
WHERE (STATUS = 1 OR STATUS = 2)
AND (start_date = '2015-11-29'
OR end_date <= '2016-01-10'
OR end_date >= '2016-01-10')
Or something that is proper according to your logic.
The right way to do it would be using the in clause for status.
Documentation: Here
SELECT *
FROM workshop_planning_history
WHERE STATUS in (1,2)
AND start_date =2015 -11 -29
Also why use end_date if you use both <= & >= operators?
You need to add parentesis in between AND condition of status and date comparison also its good idea to use date modifier when comparing dates
SELECT *
FROM workshop_planning_history
WHERE (STATUS =1
OR STATUS =2)
AND (date(start_date) = date '2015 -11 -29'
OR date(end_date) <=date '2016 -01 -10'
OR date(end_date) >=date '2016 -01 -10')
I have got a table with two columns. The first one ("val") is a integer, the second a timestamp ("ts").
Now I want to calculate the difference between the first and the last value of a given timespan.
SELECT MAX(val) - MIN(val) AS difference WHERE ts >= '2015-01-01 00:00:00' AND ts <= '2015-01-07 23:59:59'
This one is not sufficient, because in the course of time the values can exceed/undercut the first and the last value.
Example:
Day 1: 100
Day 2: 120
Day 3: 110
Day 4: 98
Day 5: 105
Day 6: 112
Day 7: 110
The difference is 110 (Day 7) minus 100 (Day 1) = 10. Not Max(val) = 120 minus Min(val) = 98 = 22
Thanks!
Join to a subquery that returns the first and last dates and join using those, and use some simple arithmetic to calculate the difference using sum():
select sum(val * case ts when ts1 then -1 else 1 end) diff
from data
join (select min(ts) ts1, max(ts) ts2
from data
where ts between ? and ?) x
on ts in (ts1, ts2)
See demo (demo schema has been simplified to isolate the essence of the solution).
One method is to use two subqueries, one that gets the first value and one that gets the last value:
SELECT ((SELECT val
FROM table t
WHERE ts >= '2015-01-01 00:00:00' AND ts <= '2015-01-07 23:59:59'
ORDER BY val DESC
LIMIT 1
) -
(SELECT val
FROM table t
WHERE ts >= '2015-01-01 00:00:00' AND ts <= '2015-01-07 23:59:59'
ORDER BY val ASC
LIMIT 1
)
) as difference
I need to select rows from table, where e.g. time is >= 18:00:00 no matter of date. Problem is that value is datetime type so there is also date beside. e.g. 2012-01-25 18:00:00.
table1
======
row 1: id='1' datetime='2012-01-25 18:00:00'
row 2: id='2' datetime='2012-01-27 15:00:00'
row 3: id='3' datetime='2012-01-30 19:45:00'
I need to select row 1 and row 3.
Is there way to combine LIKE and >= to do time >= '% 18:00:00' where % represents whatever date?
You can use the TIME() function:
WHERE TIME(`datetime`) >= '18:00:00'
select *
from table1
where HOUR(datimetime) >= 18
Something like this maybe:
SELECT *
FROM yourTable
WHERE
EXTRACT(HOUR FROM YourDate)>18