mysql query problem - mysql

id url start end
1 http://yahoo.com 2010-10-17 2010-10-10
2 http://google.com 2010-10-15 2010-12-11
3 http://espan.com 2010-10-20 2011-01-20
4 http://espan.com 2010-10-01 2011-01-01
if Today is 2010-10-16..
how can I get results work in today.
2 http://google.com 2010-10-15 2010-12-11
4 http://espan.com 2010-10-01 2011-01-01

SELECT id, url, start, end
FROM Your_Table
WHERE 2010-10-16 BETWEEN start AND end
Replace 2010-10-16 with CURRENT_TIMESTAMP or equivalent in MySQL

SELECT * FROM your_table WHERE CURDATE() >= start AND CURDATE() <= end

Something like this?
SELECT *
FROM that_table
WHERE CURRENT_TIMESTAMP BETWEEN start AND end
Note that:
in the above example, both dates are "inclusive"
in the above example, CURRENT_TIMESTAMP includes the "time" part as well

Related

How to add timestamp with interval starting from Current time with interval 1 minutes for every row in MYSQL?

In example, I have something like,
Number_index
1
2
3
4
etc
for example, using NOW() will give time stamp "10/24/2022 12:00:00"
I want to add timestamp which will be something like
Number_index
Time_Stamp
1
10/24/2022 12:00:00
2
10/24/2022 12:01:00
3
10/24/2022 12:02:00
4
10/24/2022 12:03:00
etc
etc
Would you please explain how to do that in mysql?
Thank you
We can use the TIMESTAMPADD() function here:
SELECT Number_index,
TIMESTAMPADD(MINUTE, Number_index - 1, NOW()) AS Time_Stamp
FROM yourTable
ORDER BY Number_index;
The above is one option if you want view your output as described. If instead you want to update your table, then use:
UPDATE yourTable
SET Time_Stamp = TIMESTAMPADD(MINUTE, Number_index - 1, NOW());

Mysql getting certian day's date with date range (syntax error)

This is my full code but when i start it,
DECLARE #StartDateTime DATETIME
DECLARE #EndDateTime DATETIME
SET #StartDateTime = '2022-04-01'
SET #EndDateTime = '2022-04-29';
WITH DateRange(Dates, DateWD) AS
(
SELECT #StartDateTime as Date, DATEPART(WEEKDAY, #StartDateTime)
UNION ALL
SELECT DATEADD(d,1,Dates), DATEPART(WEEKDAY, DATEADD(d,1,Dates))
FROM DateRange
WHERE Dates < #EndDateTime
)
SELECT Dates, DateWD
FROM DateRange
WHERE DATEWD NOT IN(1,7) AND Dates NOT IN(
SELECT (HOLI_YEAR + '-' + HOLI_MONTH + '-' + HOLI_DAY) AS DATE
FROM TB_HOLIDAY_CODE
OPTION (MAXRECURSION 0)
This error cames out.
I want to show 2022, april's date list except SUNDAY and SATURDAY
eg) start date is 2022-04-01
end date is 2022-04-30
so result come out ->
Dates
DateWD
2022-04-01
(FRI)
2022-04-04
(MON)
2022-04-05
(TUE)
2022-04-06
(WED)
2022-04-07
(THU)
2022-04-08
(FRI)
2022-04-11
(MON)
....
...
How can i fix this code? help me please. Thank you
*** I will not use table. Because i don't have table, and i want to use only SQL QUERY.
The syntax error is because that example is written for SQL Server, not MySQL. It needs a few adjustments to work with MySQL 8.x:
You don't need to DECLARE the user defined variables. Just use SET to both declare and assign the variable values
DATEADD() is a SQL Server function. The MySQL equivalent is DATE_ADD(date,INTERVAL expr unit)
DATEPART(weekday,...) is a SQL Server function. For MySQL, try DAYOFWEEK(date)
Lastly, use the keyword RECURSIVE with the CTE. From the docs:
A common table expression is recursive if its subquery refers to its
own name. The RECURSIVE keyword must be included if any CTE in the
WITH clause is recursive.
SQL
SET #StartDateTime = '2022-04-01';
SET #EndDateTime = '2022-04-29';
WITH RECURSIVE DateRange(Dates, DateWD) AS
(
SELECT #StartDateTime, DayOfWeek(#StartDateTime)
UNION ALL
SELECT DATE_ADD(Dates, INTERVAL 1 DAY), DayOfWeek(DATE_ADD(Dates, INTERVAL 1 DAY))
FROM DateRange
WHERE Dates < #EndDateTime
)
SELECT *
FROM DateRange
WHERE DateWDNOT IN(1,7)
Result:
Dates
DateWD
2022-04-01
6
2022-04-04
2
2022-04-05
3
2022-04-06
4
2022-04-07
5
2022-04-08
6
2022-04-11
2
2022-04-12
3
2022-04-13
4
2022-04-14
5
2022-04-15
6
2022-04-18
2
2022-04-19
3
2022-04-20
4
2022-04-21
5
2022-04-22
6
2022-04-25
2
2022-04-26
3
2022-04-27
4
2022-04-28
5
2022-04-29
6
db<>fiddle here

MySQL date_add behaviour

I've written some SQL to give me a range of dates between two times like so:
select date_add(x.min_date, interval ((t500.id-1) * 30) minute) period
from (
select '2013-08-05T23:00' as min_date, '2013-08-06T01:00' as max_date
) x,
t500
where date_add(x.min_date, interval ((t500.id-1) * 30) minute) <= x.max_date);
Where T500 is a trivial table with column id of 1 to 500 I use for simulating a loop.
Now I expect this to return:
2013-08-05 23:00:00
2013-08-05 23:30:00
2013-08-06 00:00:00
2013-08-06 00:30:00
2013-08-06 01:00:00
and finish there. But instead it carries on until 2013-08-06 23:30:00. I tried different max dates and it always returns dates to the end of the day. Could someone explain what's happening and how to make it stop when I want?
First thing that comes to mind would be casting your date strings into a date format instead of a string for example:
cast('2013-08-05T23:00' as smalldatetime)

Mysql find date range

Given the following values YYYY-MM-DD
$start = 2012-03-21
$stop = 2012-07-15
and the following records in the DB, also YYYY-MM-DD
DATE_FROM DATE_TO PRICE
2012-01-01 2012-03-01 123
2012-03-01 2012-04-08 123
2012-04-09 2012-06-04 456
2012-06-04 2012-07-02 789
2012-07-02 2012-07-16 111
2012-07-17 2012-08-17 222
How can I select all records that fall within the range of $start - $stop? This should include all except the last and first row
Attempt below, which gets records 2 - 4 but not 5.
SELECT date_from, date_to, price
FROM periods
WHERE (date(date_from) <= '$start' OR date(date_to) <= '$stop')
You want logic something like this:
date_to >= $start AND date_from <= $stop
If this is what you are looking for:
Start-----Stop
From-------------------To
Start-----Stop
From-------------------To
Start-----Stop
From-------------------To
Notice that Start always has to come before To AND Stop always has to come after From for this overlap to occur.
Let me know if any of my assumptions are off of what you are looking for. But, either way, I always find something like this easier to write down so you can visualize and easily see the greater than/less than situations.
SELECT date_from, date_to, price
FROM periods
WHERE (date_from BETWEEN '$start' AND '$stop') AND (date_to BETWEEN '$start' AND '$stop')

Mysql DateTime compare

Hi I have a table of the following 2 records:
descript | start | end
test 1 | 2011-07-18 14:30:00 | 2011-07-18 17:00:00
test 2 | 2011-07-18 00:00:00 | 2011-07-19 00:00:00
When I tried to do a select, I can't seems to retrieve the 2nd result (test 2) which seems like clearly it is dated 19th of July.
SELECT * FROM event WHERE start >= "2011-07-18 00:00:00" AND end <= "2011-07-18 23:59:59";
Would appreciate any advise.
"2011-07-19 00:00:00" is MORE than "2011-07-18 23:59:59"
By your condition it should be less, so your query does not match test 2.
Your SQL query should be:
SELECT * FROM event
WHERE start >= "2011-07-18 00:00:00"
AND end <= "2011-07-19 00:00:00";
You can just do this :)
SELECT * FROM event
WHERE start BETWEEN '2011-07-18 00:00:00' AND '2011-07-19 00:00:00'
AND end BETWEEN '2011-07-18 00:00:00' AND '2011-07-19 00:00:00'
This results in the times in between the range you specified for start AND end
you just need to exclude column end, like
SELECT * FROM event
WHERE start>="2011-07-18 00:00:00" AND start<="2011-07-18 23:59:59";