MySQL:在DateTime记录之间选择

时间:2018-01-09 05:38:26

标签: mysql sql datetime join crud

我想获得两个日期时间条目之间的记录数。 我的表格中有一个名为created_date的列,其中包含datetime data type

我想选择在2017-01-102017-01-30

之间创建的行

我编写了以下查询,但它似乎并不包含

SELECT* FROM table WHERE created_date BETWEEN '2017-01-10' AND '2017-01-30'

3 个答案:

答案 0 :(得分:1)

您遇到的问题与日期文字2017-01-31表示日期在午夜有关。要解决此问题,请按如下方式对查询进行短语:

SELECT * FROM table WHERE created_date >= '2017-01-10' AND created_date < '2017-01-31';

这表示在2017-01-10开始之前或之后的2017-01-31开始之前或之后的任何日期。2017-01-30。这意味着包括整天 <html> <head> <script src="Scripts/Asgn_3_2.js"></script> </head> <body> <form name="registration"> <div> First Name:<input type="text" placeholder="Enter Your First Name" size="20" id="fname" /><br/><br/> Last Name:<input type="text" placeholder="Enter Your Last Name" size="20" id="lname"/><br/><br/> Gender: <input type="radio" value="Male" id="mgender" />Male &nbsp&nbsp <input type="radio" value="Female" id="fgender" />Female <br/><br/> <select> <option>AL</option> <option>CA</option> <option>FL</option> <option>IL</option> <option>KS</option> <option>MN</option> </select> <br/><br/><br/><br/> <input type="checkbox" value="I Accept the terms"/>I Accept the terms <br/><br/> <button onclick="return submitbutton()">Submit</button> </div> </form> <script> function submitbutton(){ var isValidated=true; if (document.getElementById("fname").value == 0){ document.getElementById("fname").style.backgroundColor="red"; isValidated=false; } else if (!isNaN(document.getElementById("fname").value)){ document.getElementById("fname").style.backgroundColor="red"; isValidated=false; } else{ document.getElementById("fname").style.backgroundColor="#fff"; isValidated=true; } if (document.getElementById("lname").value == 0){ document.getElementById("lname").style.backgroundColor="red"; isValidated=false; } else if (!isNaN(document.getElementById("lname").value)){ document.getElementById("lname").style.backgroundColor="red"; isValidated=false; } else {document.getElementById("lname").style.backgroundColor="#fff"; isValidated=true;} return isValidated; } </script> </html>

答案 1 :(得分:0)

要获得两个日期之间的记录计数,您可以尝试以下查询

SELECT COUNT(*) 
FROM tableName
WHERE date(created_date) >= '2017-01-10' AND date(created_date) <= '2017-01-30'

答案 2 :(得分:0)

试试这个

SELECT COUNT(1) 
FROM tableName
WHERE Cast(created_date as date) Between Cast('2017-01-10' as date) AND  Cast('2017-01-30' as date)