简体   繁体   English

计算具有相同日期的行

[英]count rows with same date

i got a table named date_use and has a column id & dateUSE .我有一个名为date_use的表,并且有一个列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.现在,如果我将使用我的 php 代码向该表提交另一个日期 2015-01-01,它将首先计算具有相同日期的行,如果它等于 3,那么我选择的日期 2015-01-01 将不会被保存,也无法被选择。

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.我想先比较我提交的日期,然后如果该日期已经在数据库中输入了 3 次,那么我提交的日期将无法保存,我将只选择其他日期。

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.然后在你的 php 代码中,如果它等于 3,则读取这个值的total ,然后不要进行插入。

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:如果已安装,您可以尝试在 phpmyadmin 中执行此操作:

SELECT count(*) FROM date_use WHERE dateUSE='2015-01-02';

after this you will figure out how to implement this in script.在此之后,您将弄清楚如何在脚本中实现这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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