MySQL select rows with date like - mysql

In MySQL I have this query
SELECT DISTINCT date, descr FROM book ORDER BY date
Date is in format yyyy-mm-dd
I want to select only the the books from January 2012. I have tried to use like but that does not work.
Any ideas?

Using DATE_FORMAT function
SELECT DISTINCT date, descr FROM book
WHERE DATE_FORMAT(date, '%Y %m') = DATE_FORMAT('2012-01-01', '%Y %m')
ORDER BY date
Or using MONTH and YEAR functions
SELECT DISTINCT date, descr FROM book
WHERE Month(date) = Month('2012-01-01')
AND Year(date) = Year('2012-01-01')
ORDER BY date;
Or using BETWEEN functions
SELECT DISTINCT date, descr FROM book
WHERE date BETWEEN '2012-01-01'
AND '2012-01-31'
ORDER BY date;
Or using <= and >= operators
SELECT DISTINCT date, descr FROM book
WHERE date >= '2012-01-01'
AND date <= '2012-01-31'
ORDER BY date;
See this SQLFiddle

You can use >= and <= operators here. Check the below code:
SELECT *
FROM book
WHERE date >= '2012-01-01' AND date <= '2012-01-31'

Try this:
SELECT DISTINCT date, descr FROM book WHERE YEAR(date) = '2012' and MONTH(date) = '1'
This works if your "date"-column is a MySQL date field.

If you are adamant that you want to use the LIKE syntax, you can convert the date to CHAR first:
SELECT DISTINCT date, descr FROM book WHERE CAST(date AS char) LIKE '2012-01%' ORDER BY date;

Using like also works. With #hims056 fiddle, you can test it:
SELECT DISTINCT ID, date FROM book
WHERE date LIKE '2012-01%'
ORDER BY date;
However, it's not usual to use a like for date filtering, for me it's more natural to use >= and <= , or between. Also, there's a performance benefit.

SELECT DISTINCT date, descr
FROM book
WHERE YEAR = DATE(NOW()) AND MONTH(date) = '1'
This will give you this years books

Related

Avg function not returning proper value

I expect this query to give me the avg value from daily active users up to date and grouped by month (from Oct to December). But the result is 164K aprox when it should be 128K. Why avg is not working? Avg should be SUM of values / number of current month days up to today.
SELECT sq.month_year AS 'month_year', AVG(number)
FROM
(
SELECT CONCAT(MONTHNAME(date), "-", YEAR(DATE)) AS 'month_year', count(distinct id_user) AS number
FROM table1
WHERE date between '2020-10-01' and '2020-12-31 23:59:59'
GROUP BY EXTRACT(year_month FROM date)
) sq
GROUP BY 1
Ok guys thanks for your help. The problem was that on the subquery I was pulling the info by month and not by day. So I should pull the info by day there and group by month in the outer query. This finally worked:
SELECT sq.day_month, AVG(number)
FROM (SELECT date(date) AS day_month,
count(distinct id_user) AS number
FROM table_1
WHERE date >= '2020-10-01' AND
date < '2021-01-01'
GROUP BY 1
) sq
GROUP BY EXTRACT(year_month FROM day_month)
Do not use single quotes for column aliases!
SELECT sq.month_year, AVG(number)
FROM (SELECT CONCAT(MONTHNAME(date), '-', YEAR(DATE)) AS month_year,
count(distinct id_user) AS number
FROM table1
WHERE date >= '2020-10-01' AND
date < '2021-01-01'
GROUP BY month_year
) sq
GROUP BY 1;
Note the fixes to the query:
The GROUP BY uses the same columns as the SELECT. Your query should return an error (although it works in older versions of MySQL).
The date comparisons have been simplified.
No single quotes on column aliases.
Note that the outer query is not needed. I assume it is there just to illustrate the issue you are having.

Get SUM of values in a column after query is ran

This is my SQL statement which pulls back all the fills I've had in a certain time frame. Is there a way to get the list to come up and also pull the SUM of all of them?
SELECT customerName, date, gallons
FROM addFill
WHERE date >= CONVERT(datetime, '3-3-2015' )
ORDER BY CONVERT(DATE, date) ASC
First, I would write your query as:
SELECT customerName, date, gallons
FROM addFill
WHERE date >= '2015-03-03'
ORDER BY date ASC;
I see no value in ordering by the date and not the date/time component. Also, you might as well just use a recognizable date format for the comparison.
If you want the sum as well, then that is tricky. One method uses rollup:
SELECT customerName, date, SUM(gallons) as gallons
FROM addFill
WHERE date >= '2015-03-03'
GROUP BY customerName, date with rollup
sums of fills per customer since '3-3-2015'
SELECT customerName, date, sum(gallons)
FROM addFill
WHERE date >= CONVERT(datetime, '3-3-2015' )
GROUP BY customerName
sum of fills for all customers since '3-3-2015'
SELECT sum(gallons)
FROM addFill
WHERE date >= CONVERT(datetime, '3-3-2015' )

Count on curdate() in mysql

I have a user_entry table which contains a date field. data type is datetime.
data base is mysql.
I want a count of current date and current month and all data of current date.
How can I get this?
I tried below query but it's not working.
select * from table
where DATE(date) = CURDATE()
SELECT date FROM `test` WHERE date = CURDATE()
or
SELECT date FROM `test` WHERE date = DATE('2016-04-04')
it's work.
if you want the number of matches:
SELECT COUNT(date) from test WHERE date = CURDATE()
What is the data type of field 'date'?
To obtain the DAY/MONTH you can use the corresponding functions
SELECT MONTH(date), DAY(date) from test
Moreover, you can use groups to create a complete report
SELECT COUNT(date), date from test GROUP BY DAY(date), MONTH(date)
i used below query and it works for me.
SELECT *
FROM `user_entry`
WHERE DATE( DATE ) = DATE( NOW( ) )

How to select year in sql statement

My code:
$results = $GLOBALS['wpdb']->get_results( 'SELECT * FROM myTable WHERE date = 2014 ORDER BY id DESC', object );
The problem is date is stored in this format: 2014-01-01
So how do I select just the year ( I don't care about month and day for the time being ).
Thanks
Use the year() function:
WHERE year(date) = 2014
or use explicit comparisons:
WHERE (date >= '2014-01-01' and date < '2015-01-01')
The latter is better because it can make use of an index on the date column.
Try this Query :
SELECT * FROM myTable WHERE year(`date`)='2014' ORDER BY id DESC
Try this:
SELECT * FROM myTable WHERE date >= '2014-01-01 00:00:00' ORDER BY id DESC
To select all rows where the year of a date column (called date_col) is equal to 2014 use the year function:
SELECT * FROM `tbl` WHERE Year(`date_col`) = '2014';
You can select year for get posts with query_posts(); parameter is year. Example: query_posts("year=2014"); This is not full question for you, only alternative..

MySQL select only 1 per match

I have a MySQL table with 5 rows:
email
message_id
date
time
And I would like to count the number of emails there is per days.
So far I have this
SELECT Date,COUNT(*) AS Num FROM mail2_mailing_log WHERE Id_message=#Id GROUP BY Date ORDER BY Date DESC
But we found out that there was a problem with a script and many data got multiplied (Which have since been fixed), but I would like to be able to use the data I have.
So basically I would want to "merge" all the rows that match per email, date and time and then group by date and count the number of items.
SELECT q.date, COUNT(*)
FROM (SELECT DISTINCT email, date, time
FROM mail2_mailing_log
WHERE Id_Message = #Id) q
GROUP BY q.date
ORDER BY q.date DESC
Use the distinct keyword:
SELECT DISTINCT Date,COUNT(*) AS Num FROM mail2_mailing_log WHERE Id_message=#Id GROUP BY Date ORDER BY Date DESC
If the Date column contains the time too, then you will need to format the Date column using the DATE_FORMAT() function.
SELECT DATE_FORMAT(Date, '%W %M %Y'),COUNT(*) AS Num FROM mail2_mailing_log
WHERE Id_message=#Id GROUP BY DATE_FORMAT(Date, '%W %M %Y')
ORDER BY DATE_FORMAT(Date, '%W %M %Y') DESC