I have some code with me:
$sql = "SELECT * FROM palash ORDER BY id DESC LIMIT 31";
I want the LIMIT 31 to be LIMIT END OF THE MONTH
Use the LAST_DAY() function.
Takes a date or datetime value and returns the corresponding value for
the last day of the month. Returns NULL if the argument is invalid.
mysql> SELECT LAST_DAY('2003-02-05');
-> '2003-02-28'
mysql> SELECT LAST_DAY('2004-02-05');
-> '2004-02-29'
mysql> SELECT LAST_DAY('2004-01-01 01:01:01');
-> '2004-01-31'
mysql> SELECT LAST_DAY('2003-03-32');
-> NULL
In your case do this:
SELECT *
FROM palash
WHERE your_date_column <= LAST_DAY(CURDATE())
ORDER BY id DESC
Avoid solutions like OlivierH's (no offense, Olivier). Functions applied on your columns make it impossible for MySQL to use an index.
You can limit to current month with this conditions :
SELECT *
FROM palash
WHERE MONTH(my_date_column) = MONTH(CURDATE())
AND YEAR(my_date_column) = YEAR(CURDATE())
You can then adjust it to get previous dates too.
EDIT / Other way : to stay close to your original idea and to still have a full SQL solution, you can get number of days in current month with LAST_DAY :
DAY(LAST_DAY(my_date_column))
Here is one way you can do it
CREATE TABLE [dbo].[Dates](
[DateColumn] [datetime] NULL
) ON [PRIMARY]
INSERT INTO [dbo].[Dates]
([DateColumn])
VALUES
('2015-01-04 13:00:00'),
('2015-01-06 13:00:00'),
('2015-02-05 14:00:00')
GO
----Last Day of Current Month
Declare #LastDayOfCurrentMonth datetime = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))
SELECT *
FROM Dates
Where DateColumn < #LastDayOfCurrentMonth
Related
I have a table (Hire Rates) which contains multiple records. I'm trying to find records based on a date query but cannot get it to work correctly.
Essentially I am looking for records that contain a date between two dates. If my query date is "2021-08-15", it should return the third row (3000) as the query date falls between the two dates.
It is mostly working for me, except if the query date is equal to the start date or end date - in that case it doesn't return any result.
Table
startDate
endDate
hireRate
2021-01-01
2021-03-05
2350
2021-03-06
2021-04-08
2890
2021-04-09
2021-09-15
3000
Query
$sql = "SELECT rate, currencyID FROM hire_rates WHERE status = '1' AND NOT (startDate >= '$queryDate' OR endDate <= '$queryDate')
Try adding 'between' condition.
Like
select rate, currencyId from hire_rates where states =1 and not (cast($queryDate as DATE) between cast(startDate As DATE) and cast(endDate AS DATE) )
You would pass in the date you care about as a parameter. Then you can use between (based on your description of the logic):
SELECT rate, currencyID
FROM hire_rates
WHERE status = 1 AND
? BETWEEN startDate AND endDate;
Where ? is a parameter for the date you are passing in.
For 2021-08-15, you can write:
WHERE status = 1 AND
'2021-08-15' BETWEEN startDate AND endDate;
If you want the current date, you can actually get that from the database:
SELECT rate, currencyID
FROM hire_rates
WHERE status = 1 AND
curdate() BETWEEN startDate AND endDate;
Just try this then, `$sql = SELECT hireRate FROM hire_rate WHERE startDate<= '$queryDate' and endDate >= '$queryDate'
I use MariaDB and have a table where each row has a date and a score.
I want to first show the rows where the date is 3 days old or newer, sorted by the score - then show the rest (more than 3 days old) sorted by date.
Since my date is stored in unix time, it's fairly easy to have php calculate 3 days from before now and use that as my $scoreTimeLimit variable in the below:
Here are my two queries:
SELECT * FROM myTable WHERE myDate > $scoreTimeLimit ORDER BY myPopularityScore DESC
SELECT * FROM myTable WHERE myDate < $scoreTimeLimit ORDER BY myDate DESC
However, I would VERY much like to have only 1 query instead of two. Can it be done...?
This is a job for UNION.
SELECT * FROM (
SELECT 0 ord1, NOW() as ord2, *
FROM myTable WHERE myDate > NOW() - INTERVAL 3 DAY
UNION ALL
SELECT 1 ord1, myDate as ord2, *
FROM myTable WHERE myDate <= NOW() - INTERVAL 3 DAY
) a
ORDER BY ord1, ord2 DESC, myPopularityScore
The inner query gives you a single result set with a couple of extra columns added on to help you manage your sorting.
I have a table like this
my table is here
I need to fetch the table such that the valid_from date is less than the the date which I have(date cannot be current date).
For example. If my date is 02-04-2015, I should get the row with id 120.
Plz help me to do this in php
Please try below quewry :
$input_date = "02-04-2015";
$your_date = date("Y-m-d",strtotime($input_date));
$sql = "SELECT * FROM `table_name` where DATE(validFrom) < '".$your_date."'";
as per your example
SELECT * FROM `your_table_name` where validFrom < '02-04-2015' order by validFrom desc LIMIT 1
or
SELECT * FROM `your_table_name` where validFrom < '02-04-2015' order by validFrom asc LIMIT 1
set order by as per your expectation of output
here you want to pass date as per your date format
Using PHP/MySQL
I'm trying to create a select statement that gets the data from the least day of the current week (I'm using it to show data on a certain player 'this week'). The week starts on Sunday. Sundays's data may not always exist therefore if the Sunday data isn't found then it would use the next earliest day found, Monday, Tuesday, etc.
My date column is named 'theDate' and the datatype is 'DATE'
The query would need to be something like:
SELECT *
FROM table_name
WHERE name = '$username'
AND [...theDate = earliest day of data found for the current week week]
LIMIT 1
It would return a single row of data.
This is a query I tried for getting the 'this week' data, It doesn't seem to work correctly on Sunday's it shows nothing:
SELECT *
FROM table_name
WHERE playerName = '$username'
AND YEARWEEK(theDate) = YEARWEEK(CURRENT_DATE)
ORDER BY theDate;
This is the query that I'm using to get 'this months' data and it works even if the first day of the months data is not found, it will use the earliest date of data found in the current month/year (this query works perfect for me):
SELECT *
FROM table_name
WHERE playerName = '$username'
AND theDate >= CAST( DATE_FORMAT( NOW(),'%Y-%m-01') AS DATE)
ORDER BY theDate
LIMIT 1
Without trying this, you probably need an inner query:
select *
from table_name tn
where tn.the_date =
(select min(the_date)
from table_name
where WEEKOFYEAR(the_date) = WEEKOFYEAR(CURDATE())
and YEAR(the_date) = YEAR(CURDATE()))
viz, give me the row(s) in the table with a date equal to the earliest date in the table in the current week and year.
Try this
SELECT * FROM table_name WHERE name = '$username'
AND your_data IS NOT NULL
AND WEEK(the_date,0 = WEEK(NOW(),0))
ORDER BY DATE_FORMAT(the_date,'%w') ASC
Try the following, replace YOUR_DATE with the date from the column you want (theDate):
SELECT ADDDATE(YOUR_DATE, INTERVAL 1-DAYOFWEEK(YOUR_DATE) DAY)
FirstDay from dual
Did you try:
SELECT ADDDATE(theDate , INTERVAL 1-DAYOFWEEK(theDate ) DAY) FirstDay
FROM table_name
WHERE playerName = '$username'
ORDER BY theDate DESC
LIMIT 1
Use PHP and MySQL. In my table, there is date field (datetime) recorded by NOW() sql function. Example value of data in this field is 2010-10-07 10:57:36. How can I SELECT all data which day-month-year is today. I try to use code as below:
SELECT * FROM table WHERE date=????
Try this:
SELECT * FROM table WHERE date > CURDATE();
CURDATE() will return the current date as 2011-10-07 which will be cast to 2011-10-07 00:00:00 when comparing datetimes to it.
Note that if you use DATE(date) = CURDATE() you will run a date conversion for every row in the table, which will be really bad for your perfomance if you have many rows and/or you need to run the query often. Also make sure you have an index on date, otherwise both methods will be even slower.
SELECT * FROM table where DATE(date)=CURDATE()
SELECT * FROM tableName WHERE DATE(fieldDate) = DATE(NOW());
The date_format function allows you to easily switch between various granularities:
Select everything from the same day:
select * from table
where date_format(date, '%Y-%m-%d') = date_format(now(), '%Y-%m-%d');
From the same month:
select * from table
where date_format(date, '%Y-%m') = date_format(now(), '%Y-%m');
From the same year:
select * from table
where date_format(date, '%Y') = date_format(now(), '%Y');
From the same hour:
select * from table
where date_format(date, '%Y-%m-%d %H') = date_format(now(), '%Y-%m-%d %H');
and so on.
Try this
SELECT * FROM table WHERE DATE(my_date)=DATE(now())
my_date -> column name
SET #day = '2017-12-12' ;
SELECT * FROM table WHERE dateColumn BETWEEN DATE(#day) AND DATE_ADD(DATE(#day), INTERVAL 1 DAY ) ;
use something like this it exactly works on my code(access database):
select * from Table t where t.column>=Date() and t.column< Date() + 1
use between.
select * from table date between '2010-10-06' and '2010-10-08';