SQL 选择具有日期条件的查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4183180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select query with date condition
提问by help_pls
I would like to retrieve the records in certain dates after d/mm/yyyy
, or after d/mm/yyyy
and before d/mm/yyyy
, how can I do it ?
我想检索某个日期之后d/mm/yyyy
或之后d/mm/yyyy
和之前的记录d/mm/yyyy
,我该怎么做?
SELECT date
FROM table
WHERE date > 1/09/2008;
and
和
SELECT date
FROM table
WHERE date > 1/09/2008;
AND date < 1/09/2010
It doesn't work.
它不起作用。
回答by smirkingman
Be careful, you're unwittingly asking "where the date is greater than one divided by nine, divided by two thousand and eight".
小心,你在不知不觉中问“日期大于一除以九,除以二千零八的地方”。
Put #
signs around the date, like this #1/09/2008#
#
在日期周围打上标志,像这样#1/09/2008#
回答by onedaywhen
The semicolon character is used to terminate the SQL statement.
分号字符用于终止 SQL 语句。
You can either use #
signs around a date value or use Access's (ACE, Jet, whatever) cast to DATETIME
function CDATE()
. As its name suggests, DATETIME
always includes a time element so your literal values should reflect this fact. The ISO date format is understood perfectly by the SQL engine.
您可以#
在日期值周围使用符号,也可以使用Access 的(ACE、Jet 等)强制转换为DATETIME
function CDATE()
。顾名思义,DATETIME
始终包含一个时间元素,因此您的文字值应反映这一事实。SQL 引擎完全理解 ISO 日期格式。
Best not to use BETWEEN
for DATETIME
in Access: it's modelled using a floating point type and anyhow time is a continuum ;)
最好不要使用BETWEEN
用于DATETIME
在Access:它使用一个浮点型建模,反正时间是连续的;)
DATE
and TABLE
are reserved words in the SQL Standards, ODBC and Jet 4.0 (and probably beyond) so are best avoided for a data element names:
DATE
和TABLE
是 SQL 标准、ODBC 和 Jet 4.0(可能还有更高版本)中的保留字,因此最好避免用于数据元素名称:
Your predicates suggest open-open representation of periods (where neither its start date or the end date is included in the period), which is arguably the least popular choice. It makes me wonder if you meant to use closed-open representation (where neither its start date is included but the period ends immediately prior to the end date):
您的谓词建议期间的开放式表示(其开始日期或结束日期都不包含在期间内),这可以说是最不受欢迎的选择。这让我想知道您是否打算使用封闭开放的表示法(既不包括开始日期,又不包括该期间在结束日期之前立即结束):
SELECT my_date
FROM MyTable
WHERE my_date >= #2008-09-01 00:00:00#
AND my_date < #2010-09-01 00:00:00#;
Alternatively:
或者:
SELECT my_date
FROM MyTable
WHERE my_date >= CDate('2008-09-01 00:00:00')
AND my_date < CDate('2010-09-01 00:00:00');
回答by dnyaneshwar
select Qty, vajan, Rate,Amt,nhamali,ncommission,ntolai from SalesDtl,SalesMSt where SalesDtl.PurEntryNo=1 and SalesMST.SaleDate= (22/03/2014) and SalesMST.SaleNo= SalesDtl.SaleNo;
That should work.
那应该工作。
回答by MIDHUN H
hey guys i think what you are looking for is this one using select command. With this you can specify a RANGE GREATER THAN(>) OR LESSER THAN(<) IN MySQL WITH THIS:::::
嘿伙计们,我认为您正在寻找的是使用 select 命令的这个。有了这个,你可以在 MySQL 中指定一个 RANGE GREATER THAN(>) 或 LESSER THAN(<) WITH THIS:::::
select* from <**TABLE NAME**> where year(**COLUMN NAME**) > **DATE** OR YEAR(COLUMN NAME )< **DATE**;
FOR EXAMPLE:
例如:
select name, BIRTH from pet1 where year(birth)> 1996 OR YEAR(BIRTH)< 1989;
+----------+------------+
| name | BIRTH |
+----------+------------+
| bowser | 1979-09-11 |
| chirpy | 1998-09-11 |
| whistler | 1999-09-09 |
+----------+------------+
FOR SIMPLE RANGE LIKE USE ONLY GREATER THAN / LESSER THAN
对于简单的范围,例如仅使用大于/小于
mysql> select COLUMN NAMEfrom <TABLE NAME> where year(COLUMN NAME)> 1996;
mysql> select COLUMN NAMEfrom < TABLE NAME> where year( COLUMN NAME)> 1996;
FOR EXAMPLE mysql>
例如 mysql>
select name from pet1 where year(birth)> 1996 OR YEAR(BIRTH)< 1989;
+----------+
| name |
+----------+
| bowser |
| chirpy |
| whistler |
+----------+
3 rows in set (0.00 sec)