I'm using mysql. I have a table called orders with the fields orderid, orderdate, customerid, shippedcity and amount.
I want to show orders for the month of June and year 2011. Please help.
You can use BETWEEN as below:
SELECT *
FROM ORDER
WHERE ODERDATE BETWEEN DATE ('01-06-2011') AND DATE ('30-06-2011');
this one is fairly easy :-)
SELECT *
FROM orders
WHERE MONTH(orderdate) = 6
AND YEAR(orderdate) = 2011
For better performance of the query you could use this one
SELECT *
FROM orders
WHERE orderdate BETWEEN '2011-06-01 00:00:00' AND '2011-06-30 23:59:59'
Cheers -Sven
Try this one,
SELECT *
FROM tableName
WHERE MONTH(orderdate) = 6 AND
YEAR(orderDate) = 2011
for reference,
MONTH()
YEAR()
well, first of all i have to ask, does the orderdate field contains full date (dd/mm/yyyy)? or how is the date stored?
you could try SELECT orderid FROM orders WHETE orderdate = 'your date in the correct format it is stored'
Related
This is my SQL to fetch orders of current week:
SELECT * FROM orders
WHERE YEARWEEK(order_date, 1) = YEARWEEK(CURDATE(), 1)
ORDER BY order_date DESC;
But the problem is that it also selects records of future dates in the current week. How to stop that?
Just add a condition on the current date as well:
SELECT o.*
FROM orders o
WHERE YEARWEEK(order_date, 1) = YEARWEEK(CURDATE(), 1) AND
order_date <= CURDATE()
ORDER BY order_date DESC;
Be aware: if you use the order_date column in a function to build the where clause, this will result in a query which does not use an index on order_date and so should be avoided if your table can be big.
If you calculate the first date of the current week you can build your where clause just using order_date and it will use an index.
Maybe this can help to inspire you:
WHERE order_date BETWEEN DATEADD(DAY, -WEEKDAY(CURDATE())), CURDATE()) AND CURDATE()
I haven't tested this but adapted the function names to MySQL using the documentation. The idea is to calculate the first date of the current week so you know the range to filter.
I have a table with 4 columns:
1. customerID
2. dateAdded
3. productID
4. quantity
The format of dateAdded is like this: 20180730 (YearMonthDay).
I want to group the rows in the table by year and month.
I tried the code below but it doesn't work. I still see rows with the same year and month repeated, but different day.
SELECT dateAdded
, SUM(quantity)
FROM testTable
GROUP
BY DATE_FORMAT(dateAdded, '%Y%m')
, dateAdded
ORDER
BY dateAdded DESC
Any idea how to fix this?
Thank you
Do not group by what you do not want the data to be grouped. Since you provide dateAdded as a parameter to be grouped with, it splits the data by dateAdded and you gain nothing out of year-month grouping. Remove the columns from select/groupby that you use for grouping like this:
SELECT DATE_FORMAT(dateAdded, '%Y%m')
, SUM(quantity)
FROM testTable
GROUP BY
DATE_FORMAT(dateAdded, '%Y%m')
If you store dateAdded as 20180730, looks like it's a string, so it wont work with DATE_FORMAT function, you can use SUBSTRING instead
something like
SELECT dateAdded, SUM(quantity), SUBSTRING(dateAdded, 1, 6) as d
FROM testTable
GROUP BY d
ORDER BY dateAdded DESC
use year and month function
SELECT Year(dateAdded) as YearOfDate,Month(dateAdded) as MonthOfdate,
, SUM(quantity) as Qty
FROM testTable
GROUP
BY Year(dateAdded), Month(dateAdded)
ORDER BY dateAdded DESC
I want to display the records using two different date. I tried using the Between
select * from billing where select_client = '2' and order_date BETWEEN '01/06/2018' and '30/06/2018' order by id ASC
It returns JULY month records also. I tried used >= and <=. That query also returns same record.
select * from billing where select_client = '2' and order_date >= '01/06/2018' and order_date <= '30/06/2018' order by id ASC
Kindly help me out to get only the records between the two dates. Thanks in advance
You have to convert the strings to a date to compare it:
select * from billing where select_client = '2' and STR_TO_DATE(order_date, '%d/%m/%Y') BETWEEN STR_TO_DATE('01/06/2018','%d/%m/%Y') and STR_TO_DATE('30/06/2018','%d/%m/%Y') order by id ASC
wrong date format. if order_date is mysql DATE then format should be 2018-06-01
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..
For example I have a table with fields:
id date
1 2001-01-01
2 2001-01-05
.................
N 2011-12-31
How get i get all months last days from this table?
example:
if i have dates like 2001-05-31 and 2001-06-01
i need only 2001-05-31 not both
You can do SELECT LAST_DAY for example the below returns Oct. 31st. 2010
SELECT LAST_DAY('2010-10-10');
select max(date_format(date, '%d')) as last_day_of_the_month
from table
group by date_format(date, '%Y%m')
maybe this would work better?
select DISTINCT(LAST_DAY(date)) from table GROUP BY date_format(date, '%Y%m')
SELECT id, date
FROM `table`
WHERE DATE( date ) = LAST_DAY( date )
The DATE function over field is for filter the date without time, use only if you have a datetime column.
The query get all rows with date = last day of month.
select subdate(adddate(subdate(`date`, day(`date`) - 1), interval 1 month), 1)
Note: This is the "hard way". See #harper89's answer - it's better :)
I found a solution. but this query is very slow on large tables. so I am still looking for a better solution
select DISTINCT(LAST_DAY(date)) from table;
select max(date)
from table
group by year(date), month(date)