count rows with same date - php

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.

Related

How to find last date all record in MySQL?

how can i find last date record in MySQL.
i have this current date record but if i want to get a data of last day (not yesterday). it is possible to get last date of record in MySQL.
my current date record are there (28-11-2017)..
and i want last date of record which is (25-11-2017)
between this 25 to 28 there is no data . i want this last date record.
ex.
id DATE PRODUCT_ID
1 2017-11-25 11
2 2017-11-25 12
3 2017-11-24 6
so last date in my table will be 25. how can i get all record of that date.
link for current date table: current table
One option is to compare the date of each record against a non correlated subquery which finds the most recent date in the table.
SELECT *
FROM yourTable
WHERE DATE = (SELECT MAX(DATE) FROM yourTable);
If you are certain that there is only one latest date, or you can live with just a single record in the event of ties, then MySQL offers an even simpler option:
SELECT *
FROM yourTable
ORDER BY DATE DESC
LIMIT 1;
SELECT DATE FROM your_table ORDER BY id DESC LIMIT 1
This will get you the last date.

get data between start date and end date

Get data between start date and end date
I am getting data but my requirement is little bit different pls check the below database
I have two text boxs
1) start date
2) end date
Database is like this
table name->purchase_order
sno start_date end_date
1 2017/08/01 2017/12/01
2 2017/08/01 2017/11/30
3 2017/09/01 2017/09/30
4 2017/09/01 2017/10/30
5 2017/10/01 2017/11/30
I am trying like this
select *
from purchase_order
where start_date>= '2017/09/01' and start_date<= '2017/09/01'
OR end_date>= '2017/09/01' and end_date<= '2017/09/01'
Output i am getting
sno start_date end_date
3 2017/09/01 2017/09/30
4 2017/09/01 2017/10/30
What i require
if i select between this 2017/09/01 - 2017/09/30 i want out put like this {in id "1" in between 8th month to 12th month there is 9th month is there so it also has to come}
sno start_date end_date
1 2017/08/01 2017/12/01
2 2017/08/01 2017/11/30
3 2017/09/01 2017/09/30
4 2017/09/01 2017/10/30
thanks
Instead of checking that the column values are between your dates, you want to see if your dates are between the column values. Using BETWEEN makes the logic look a little cleaner:
select * from purchase_order where '2017/09/01' BETWEEN start_date AND end_date
If you select 2017/09/01 - 2017/09/30, then do this:
select * from purchase_order where '2017/09/01' BETWEEN start_date AND end_date AND '2017/09/30' BETWEEN start_date AND end_date
AND will make sure both dates are between start_date and end_date, OR will make sure at least one of the dates is between the columns.
You would not want to have 4 clauses as you have in your question, you would want to have just 2.
The logic should be as follows:
the start date from the input in the application is later or equal to the start date in the start column in your table
the end date from the input in the application is earlier or equal to the end date in the end column in your
application.
Based on this logic, you should be able to use the following query:
SELECT
*
FROM purchase_order
WHERE start_date<= '2017/09/01' -- start date input
AND end_date<= '2017/09/01' -- end date input
One may complain that I have a hole in my logic due to the fact that I don't check to see that the end date is later than the start date, though you would want to do that at the application level, hence it is not relevant at the database level where this question was asked.

create mysql query for fetching data in 3 days slotwise

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.

Select continues dates from table

This is little confusing me how to select dates from table on continuous basis.
Suppose i have absent_report table and have entries like this,
Sno Code date
1 101 01-01-2014
2 101 02-01-2014
3 101 03-01-2014
4 101 05-01-2014
5 101 06-01-2014
6 101 07-01-2014
I only want to select continues date from a date. like first three dates not fourth one and so on.
Example Like I have date from which I have to compare is Like 31-12-2013 now from here next continues dates like 01-01-2014,02-01-2014,03-01-2014 only, No matter next records like i have dates in that table 05-01-2014,06-01-2014,07-01-2014. I just want first continuous dates.Hope this clear more what I want.
Use the BETWEEN syntax
SELECT * FROM table WHERE date BETWEEN '2014-01-01' AND '2014-01-03';
You can check if either the next row has the following day or the previous row has the previous day. To do this, I used DATE_ADD() as described in this answer. However, I am not sure if this would be better than implementing the logic in PHP alltogether.
SELECT sno, code, date FROM table AS current
WHERE
DATE(DATE_ADD(date, INTERVAL -1 DAY)) = (SELECT date FROM table AS previous WHERE previous.date < current.date ORDER BY date DESC LIMIT 1)
OR
DATE(DATE_ADD(date, INTERVAL +1 DAY)) = (SELECT date FROM table AS next WHERE next.date > current.date ORDER BY date ASC LIMIT 1)
ORDER BY date ASC
See it work here.

selecting the data between two date

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

Categories