I am trying to select the price between two date (star date and end date). In my database i have fixed price like this:
2013-05-01 to 2013-05-31 price: 300
2013-06-01 to 2013-06-30 price: 200
2013-07-01 to 2013-07-01 price: 250
I user selects any date between these 3 i am able to generate result by doing
sdate>='2013-05-01'
AND edate <='2013-05-31'`.
But if a user selects date 2013-05-28 to 2013-06-03 then it should two days price (300) for may month and 3 days price (200) from the month june.
How can i achieve this?
try this
select price from tablename where sdate >= '2013-05-01' and edate <= '2013-05-31'
if user enter date for two different months then your select query will return two prices. assume select price from tb_demo where sdate>='2013-06-01' and edate<='2013-07-31' this will return two prices which is stored into resultset.
After that by using while loop you can easy print those price.If u want to print month name also then use array and print that name.
Please Before positing Use search...
SQL query to select dates between two dates
Related
I have a table called sales which is displayed below:
s.no item_sold date
1 soap 25.07.2017
2 bisket 19.07.2017
3 chocklate 26.06.2017
4 milk 26.06.2016
Use subdate(current_date, 1)
and BETWEEN.
Eg. To get records between yesterday and day before yesterday.
You can do something like this:
SELECT .. FROM ... WHERE your_date_column BETWEEN
subdate(current_date, 1) AND subdate(current_date, 2)
Uh... I guess...
select * from sales where date between '2017-06-01' and '2017-07-31'
But I think your field name "date" and "s.no" are going to cause you problems...
i got a table named date_use and has a column id & dateUSE.
id dateUSE
1 2015-01-01
2 2015-01-01
3 2015-01-01
4 2015-01-02
5 2015-01-02
now if i will submit another date 2015-01-01 to this table using my php code, it will first count the rows that has the same date and if it is equals to 3 then the date 2015-01-01 that i selected will not be saved and will not be available to be selected.
i want to compare first the date i submitted then if that date has already inputted 3 times in the database then the date i submitted will not be available to be saved and i will just select other date.
just like in checking a reservation date availability, i will select a date then click a button then it will check first my database to see if that date was entered three times then the date i selected will not be available.
You should do this:
SELECT
COUNT(*) AS Total
FROM tablenames
WHERE dateUSE = '$date';
Then in your php code, read this value for total if it is equal 3, then don't do the insert.
You need either a trigger for this or application level logic. Here is an example of application-level logic:
insert into t(date)
select '2015-01-01' as newdate
from (select count(*) as cnt
from t
where date = '2015-01-01'
) x
where x.cnt < 3;
You can try execute this in phpmyadmin if you have installed:
SELECT count(*) FROM date_use WHERE dateUSE='2015-01-02';
after this you will figure out how to implement this in script.
I want to create a query for fetch all the data from database. But condition is get from before current month and data will convert into 3 days slot wise with count.Like if i have 12 month data and in July 1 to 3 date data will insert into 50 rows and from 3 to 6 date data will insert into 5 rows.How i fetch this things.That is the mail problem. I have wondered but nothing found related my problem. Is this possible.
I assume you have a DATE or a DATETIME column, that marks the day of the insert, say the column created_at. Then you could get the desired result.
SELECT
COUNT(*)
FROM
your_table
WHERE
created_at BETWEEN '2014-07-01' AND '2014-07-31 23:59:59'
GROUP BY
(DAYOFMONTH(created_at) - 1) DIV 3
Explanation:
With the help of the DIV operator you get your 3-day-slots.
Note
If there's a slot without rows, it won't be in the result. To get these too, you could use a LEFT JOIN with the maximum 11 slots.
Demo
If you having timestamps as attribute type then might be this help you
SELECT count(*) as count
FROM table
AND
TIMESTAMPDIFF(DAY,date,2014-08-01)<=3";
The date is the attribute of your column, TIMESTAMPDIFF will find difference between given date if it's under 3 days from 2014-08-01 records will show up. This way by just sending one date into this position you can find 3 slots of date from your table.
I have 50 rows/entrys in my table Orders. I have a column, that holds when the order is claimed at, named claimed_at.
The date in this field are in this format: 2011-10-03 07:07:33
This is in the format (yy/mm/dd time).
I also have a column called price, this price is how much they paid.
I would like to display totals per day.
So for 6 orders from the date 2011-10-03, it should take the 6 order's price value, and plus them together.
So I can display:
2011-10-03 -- Total: 29292 Euros
2011-10-02 -- Total: 222 Euros
2011-09-28 -- Total: 4437 Euros
How can i do this?
You need to use aggregate functionality of MySQL in conjuction with some DATE conversion.
SELECT DATE(claimed_at) AS day
, SUM(price) AS total
FROM Orders
GROUP BY day
Create a new field say day with DATE_FORMAT(claimed_at,'%Y-%m-%d') AS day , sum the price and group by day so you will get the result you want.
Something like
SELECT DATE_FORMAT(claimed_at,'%Y-%m-%d') AS day,SUM(price) as total FROM orders GROUP BY day ORDER BY day DESC
Use grouping like this :
SELECT claimed_at, SUM(price) AS total
FROM Orders
GROUP BY claimed_at
You need to use function to get only date part(not time) in grouping. I dont know which function is used in mysql, in MSSQL it's CONVERT() function.
SELECT TRUNC(claimed_at), SUM(PRICE)
FROM my_orders_table
GROUP BY TRUNC(claimed_at)
I have an SQL table like this : sales(product,timestamp)
I want to display a chart using Open Flash Chart but i don't know how to get the total sales per hour within the last 12 hours. ( the timestamp column is the sale date )
By example i will end up with an array like this : array(12,5,8,6,10,35,7,23,4,5,2,16) every number is the total sales in each hour.
Note: i want to use php or only mysql for this.
Thanks
SELECT HOUR(timestamp),COUNT(*)
FROM sales
WHERE timestamp >= DATE_SUB(NOW(),INTERVAL 12 HOUR)
GROUP BY HOUR(timestamp)
The SQL is
SELECT HOUR(timestamp), COUNT(product)
FROM sales
ORDER BY HOUR(timestamp)
Loop over the result to get it into an array.
EDIT: Applying requested where condition for unix timestamp
SELECT HOUR(timestamp), COUNT(product)
FROM sales
WHERE timestamp >= UNIX_TIMESTAMP(DATE_SUB(NOW(),INTERVAL 12 HOUR))
ORDER BY HOUR(timestamp)
Some pseudo code:
foreach timestamp
use date('G',timestamp) to get hour
increment array value using the hour as key
Something along those lines. Watch out for timezones.