Improvement in the below mysql query - mysql

SELECT * FROM *****
WHERE ((first_appeared_on BETWEEN DATE_SUB( '2015-07-28' , INTERVAL 1 DAY ) AND '2015-07-28')
or (last_appeared_on BETWEEN DATE_SUB( '2015-07-28' , INTERVAL 1 DAY ) AND '2015-07-28'))
In the above query I don't want to type the date (2015-07-28) again and again Can I use a variable and assign that variable a value
day = '2015-07-28';
SELECT * FROM *****
WHERE ((first_appeared_on BETWEEN DATE_SUB(day , INTERVAL 1 DAY ) AND day)
or (last_appeared_on BETWEEN DATE_SUB( day , INTERVAL 1 DAY ) AND day))

This can be done in MySQL with a user-defined variable:
SET #day = '2015-07-28';
SELECT * FROM *****
WHERE ((first_appeared_on BETWEEN DATE_SUB( #day , INTERVAL 1 DAY ) AND #day)
or (last_appeared_on BETWEEN DATE_SUB( #day , INTERVAL 1 DAY ) AND #day));

Related

How can i used less than equal or between function FROM DUAL table?

Im trying to get the date from previous week to current and it seemd between or >= are not working here.
WITH curr_cyc_dt AS (
SELECT BETWEEN TO_DATE ('20181228', 'yyyymmdd') AND TO_DATE ('20190104', 'yyyymmdd') cyc_dt
FROM DUAL
)
set #myDate = CURDATE();
set #rowNumber = 0;
select date_sub(#myDate, interval #rowNumber day),
(#rowNumber := #rowNumber + 1) as rownum
from information_schema.columns
LIMIT 7;
BETWEEN does not work like that, it will not automagically generate a list of dates for you. It is a comparison operator that checks if a date belongs to a date interval.
The simplest way to generate a list of days for the last 7 days (until today included) :
WITH curr_cyc_dt AS (
SELECT
CURDATE(),
DATE_SUB(CURDATE(), INTERVAL 1 DAY),
DATE_SUB(CURDATE(), INTERVAL 3 DAY),
DATE_SUB(CURDATE(), INTERVAL 4 DAY),
DATE_SUB(CURDATE(), INTERVAL 5 DAY),
DATE_SUB(CURDATE(), INTERVAL 6 DAY),
DATE_SUB(CURDATE(), INTERVAL 7 DAY)
)
...
PS : FROM DUAL is superfluous in MySQL

sql take all querys from last month

I followed the first answer on this question but i have some problems with my sql query. I don´t get any result.
What error i made ?
How can i set CURDATE always on the first day of the month ?
SELECT DATE_FORMAT(`date`, '%Y-%m-%d') , `price`
FROM `sales`
WHERE `id` = :id
AND (`date` BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE())
Edit Whole code:
$verkaufmonat3 = 0;
$verkaufmonatanzahl2 = 0;
$verkaufmonat4 = array();
$sqluser5 = $X['dbh']->prepare("SELECT DATE_FORMAT(`date`, '%Y-%m-%d') , `preis` FROM `verkauf` WHERE `vertreterid` = :id AND (`date` BETWEEN CURDATE(), '%Y-%m-01' - INTERVAL 60 DAY AND CURDATE(), '%Y-%m-01')");
$sqluser5->execute(array(
':id'=>$_SESSION['id']
));
$verkaufmonatanzahl2 = $sqluser5->rowCount();
$verkaufmonat4 = $sqluser5->fetchAll();
for ($a = 0; $a <= $verkaufmonatanzahl2; $a++) {
$verkaufmonat3 += $verkaufmonat2[$a]['preis'];
}
In my database i have a sale on the 28.10.2016 wich has for ex. a value of 50 eur. So my $verkaufmonat3 should be 50 but it isn´t, it´s 0.
Your question isnt clear but to get the first day of current month then you use
SELECT DATE_FORMAT(CURDATE() , '%Y-%m-1')
So if you want the previous month data try
SELECT
DATE_FORMAT(CURDATE() , '%Y-%m-1') - INTERVAL 1 MONTH as first_day,
DATE_FORMAT(CURDATE() , '%Y-%m-1') - INTERVAL 1 DAY as last_day
MEANING
WHERE `date` BETWEEN DATE_FORMAT(CURDATE() , '%Y-%m-1') - INTERVAL 1 MONTH
AND DATE_FORMAT(CURDATE() , '%Y-%m-1') - INTERVAL 1 DAY

SELECT specific date with JOIN

Table Client contains last_name and email column while table Product contain email and expiry_date column with 13 row of records. This syntax correctly select only 2 rows from Product table.
SELECT * FROM `Product` WHERE expiry_date > DATE_ADD( CURRENT_DATE, INTERVAL 24 DAY )
AND expiry_date < DATE_ADD( CURRENT_DATE, INTERVAL 24 + 1 DAY )
Now I want to join it with Client table to select last_name with matching email. Unfortunately my JOIN syntax below select all 13 records from Product table.
SELECT * FROM `Product` JOIN `Client`
ON product.email=client.email
AND (expiry_date > DATE_ADD( CURRENT_DATE, INTERVAL 24 DAY )
AND expiry_date < DATE_ADD( CURRENT_DATE, INTERVAL 24 + 1 DAY ))
This syntax also does not work. All 13 rows from Product table was selected:
SELECT * FROM
(
SELECT * FROM `Product` WHERE expiry_date > DATE_ADD( CURRENT_DATE, INTERVAL 24 DAY )
AND expiry_date < DATE_ADD( CURRENT_DATE, INTERVAL 24 +1 DAY )
) A
LEFT JOIN Client ON A.email=client.email
WHERE A.expiry_date > DATE_ADD( CURRENT_DATE, INTERVAL 24 DAY )
AND A.expiry_date < DATE_ADD( CURRENT_DATE, INTERVAL 24 +1 DAY )
How do I select only 2 last_name and not all 13. I hope I explained it clearly. Thanks in advance.
The way you are specifying your JOIN conditions, everything matches. You should specify a WHERE clause instead
SELECT * FROM `Product` JOIN `Client`
ON product.email=client.email
WHERE expiry_date > DATE_ADD( CURRENT_DATE, INTERVAL 24 DAY )
AND expiry_date < DATE_ADD( CURRENT_DATE, INTERVAL 24 + 1 DAY )
You need to use GROUP BY to get only 2 rows instead of all rows in Product table. For example:
SELECT * FROM `Product` JOIN `Client`
ON product.email=client.email
AND (expiry_date > DATE_ADD( CURRENT_DATE, INTERVAL 24 DAY )
AND expiry_date < DATE_ADD( CURRENT_DATE, INTERVAL 24 + 1 DAY ))
GROUP BY expiry_date

Select date that's in given date's interval

I have a table of dates, and want to select dates that are in 1,5 hour interval of given date.
For example:
-- '2013-06-11 18:40' is the given date, and I want all dates that are
-- inside +/- 1.5 hour interval
SELECT datum
FROM table
WHERE date(datum) > date( date_sub( '2013-06-11 18:40', interval 1.5 hour ) )
AND date(datum) < date( date_add( '2013-06-11 18:40', interval 1.5 hour ) )
The MySQL date function takes the date part of a datetime. That's not advisable when you're looking for a 3 hour interval :)
Try:
WHERE datum between date_sub( '2013-06-11 18:40', interval 90 minute) and
date_add( '2013-06-11 18:40', interval 90 minute)
I used integer minutes instead of hours plus removed DATE function as Andomar suggested:
SELECT datum
FROM table
WHERE
datum > date( date_sub( '2013-06-11 18:40', interval 90 MINUTE ) )
AND
datum < date( date_add( '2013-06-11 18:40', interval 90 MINUTE ) )

Query to get all rows from previous month

I need to select all rows in my database that were created last month.
For example, if the current month is January, then I want to return all rows that were created in December, if the month is February, then I want to return all rows that were created in January. I have a date_created column in my database that lists the date created in this format: 2007-06-05 14:50:17.
SELECT * FROM table
WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
Here's another alternative. Assuming you have an indexed DATE or DATETIME type field, this should use the index as the formatted dates will be type converted before the index is used. You should then see a range query rather than an index query when viewed with EXPLAIN.
SELECT
*
FROM
table
WHERE
date_created >= DATE_FORMAT( CURRENT_DATE - INTERVAL 1 MONTH, '%Y/%m/01' )
AND
date_created < DATE_FORMAT( CURRENT_DATE, '%Y/%m/01' )
If there are no future dates ...
SELECT *
FROM table_name
WHERE date_created > (NOW() - INTERVAL 1 MONTH);
Tested.
Alternatively to hobodave's answer
SELECT * FROM table
WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
You could achieve the same with EXTRACT, using YEAR_MONTH as unit, thus you wouldn't need the AND, like so:
SELECT * FROM table
WHERE EXTRACT(YEAR_MONTH FROM date_created) = EXTRACT(YEAR_MONTH FROM CURDATE() - INTERVAL
1 MONTH)
SELECT *
FROM yourtable
where DATE_FORMAT(date_created, '%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 1 month),'%Y-%m')
This should return all the records from the previous calendar month, as opposed to the records for the last 30 or 31 days.
Even though the answer for this question has been selected already, however, I believe the simplest query will be
SELECT *
FROM table
WHERE
date_created BETWEEN (CURRENT_DATE() - INTERVAL 1 MONTH) AND CURRENT_DATE();
WHERE created_date >= DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1 DAY)
AND created_date <= DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 1 MONTH)), INTERVAL 0 DAY)
This worked for me (Selects all records created from last month, regardless of the day you run the query this month)
Alternative with single condition
SELECT * FROM table
WHERE YEAR(date_created) * 12 + MONTH(date_created)
= YEAR(CURRENT_DATE) * 12 + MONTH(CURRENT_DATE) - 1
select fields FROM table
WHERE date_created LIKE concat(LEFT(DATE_SUB(NOW(), interval 1 month),7),'%');
this one will be able to take advantage of an index if your date_created is indexed, because it doesn't apply any transformation function to the field value.
Here is the query to get the records of the last month:
SELECT *
FROM `tablename`
WHERE `datefiled`
BETWEEN DATE_SUB( DATE( NOW( ) ) , INTERVAL 1
MONTH )
AND
LAST_DAY( DATE_SUB( DATE( NOW( ) ) , INTERVAL 1
MONTH ) )
Regards
- saqib
if you want to get orders from last month, you can try using
WHERE MONTH(order_date) = MONTH(CURRENT_DATE()) -1
One more way to do this in:
MYSQL
select * from <table_name> where date_created >= DATE_ADD(NOW(), INTERVAL -30 DAY);
SELECT * FROM table
WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)