简体   繁体   中英

count rows with same date

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM