English 中文(简体)
Sql 多个栏目的条款
原标题:Sql Between Clause for multiple columns
  • 时间:2012-05-17 10:00:03
  •  标签:
  • sql

我试图通过使用“条款”进行询问,但在这方面存在一些问题。

S_E1:

这里的数据是43。

select RECORD_TIMESTAMP as DateRecorded, ROUND (S_E1 ,2 )as S_E1
from TBL_SENSORS
Where RECORD_TIMESTAMP Between  4/28/2012 12 :00 AM  
                           and  5/17/2012 12 :00 AM 
And ( S_E1 Between 10 And 100 )

第二张S_E:

这里的数据是68。

select RECORD_TIMESTAMP as DateRecorded, ROUND (S_E2 ,2 )as S_E2
from TBL_SENSORS
Where RECORD_TIMESTAMP Between  4/28/2012 12 :00 AM 
                           and  5/17/2012 12 :00 AM 
And ( S_E2 Between 10 And 100 )

<<>Problem>:

但是,如果把这些问题与询问合并在一起,则提供错误的数据。 这给我带来了73个增长。 我认为,它应当给111个牢房。

select RECORD_TIMESTAMP as DateRecorded, 
       ROUND (S_E2 ,2 )as S_E2, 
       ROUND (S_E1 ,2 ) as S_E1
from TBL_SENSORS
Where RECORD_TIMESTAMP Between  4/28/2012 12 :00 AM 
                           and  5/17/2012 12 :00 AM 
And ( S_E2 Between 10 And 100) 
and (S_E1 Between 10 And 100  )

请在此谈谈我错失之处。

最佳回答

使用<代码>OR而不是AND

SELECT RECORD_TIMESTAMP AS DateRecorded, 
       Round (S_E2, 2)  AS S_E2, 
       Round (S_E1, 2)  AS S_E1 
FROM   TBL_SENSORS 
WHERE  RECORD_TIMESTAMP BETWEEN  4/28/2012 12 :00 AM  AND  5/17/2012 12 :00 AM  
       or ( S_E2 BETWEEN 10 AND 100 ) 
       or ( S_E1 BETWEEN 10 AND 100 ) 

在询问中,没有保证你将获得111份记录,因为有两种情况的共同记录。

条件1 - <代码>(S_E2 BETWEEN 10 and 100)

条件2 - <代码>(S_E1 BETWEEN 10 and 100)

使用工会 查阅所有记录的所有查询(111)。

select RECORD_TIMESTAMP as DateRecorded, ROUND (S_E1 ,2 )as S_E1, 0 as S_E2
from TBL_SENSORS
Where RECORD_TIMESTAMP Between  4/28/2012 12 :00 AM  and  5/17/2012 12 :00 AM 
And ( S_E1 Between 10 And 100 )

UNION ALL

select RECORD_TIMESTAMP as DateRecorded, 0 as S_E1, ROUND (S_E2 ,2 ) as S_E2
from TBL_SENSORS
Where RECORD_TIMESTAMP Between  4/28/2012 12 :00 AM 
and  5/17/2012 12 :00 AM 
And ( S_E2 Between 10 And 100 )
问题回答

我认为,你应在这两份声明之间形成联盟,或者说你应该使用“或”而不是“或”。

select RECORD_TIMESTAMP as DateRecorded,ROUND (S_E2 ,2 )as S_E2,ROUND (S_E1 ,2 )as S_E1 from TBL_SENSORS Where RECORD_TIMESTAMP Between  4/28/2012 12 :00 AM  and  5/17/2012 12 :00 AM  AND (( S_E2 Between 10 And 100) OR(S_E1 Between 10 And 100 ))

2. 改变血压逻辑,将外心放在一边,取而代之;

select RECORD_TIMESTAMP as DateRecorded, 
       ROUND (S_E2 ,2 )as S_E2, 
       ROUND (S_E1 ,2 ) as S_E1
from TBL_SENSORS
Where RECORD_TIMESTAMP Between  4/28/2012 12 :00 AM 
                           and  5/17/2012 12 :00 AM 
And (( S_E2 Between 10 And 100) 
OR (S_E1 Between 10 And 100  ))

问题在于S_E2和S_E1的限制。

为了与大家一样工作,你必须使用<代码>。 UNION ALL:

select RECORD_TIMESTAMP as DateRecorded, ROUND (S_E1 ,2 )as S_E
from TBL_SENSORS
Where RECORD_TIMESTAMP Between  4/28/2012 12 :00 AM  and  5/17/2012 12 :00 AM 
And ( S_E1 Between 10 And 100 )

UNION ALL

select RECORD_TIMESTAMP as DateRecorded, ROUND (S_E2 ,2 )as S_E
from TBL_SENSORS
Where RECORD_TIMESTAMP Between  4/28/2012 12 :00 AM 
and  5/17/2012 12 :00 AM 
And ( S_E2 Between 10 And 100 )

或您使用<代码>OR代替AND 类似:

select RECORD_TIMESTAMP as DateRecorded, 
       ROUND (S_E2 ,2 )as S_E2, 
       ROUND (S_E1 ,2 ) as S_E1
from TBL_SENSORS
Where RECORD_TIMESTAMP Between  4/28/2012 12 :00 AM 
                           and  5/17/2012 12 :00 AM 
And ( (S_E2 Between 10 And 100) OR (S_E1 Between 10 And 100)  )

由于限制的唯一区别是S_E1和S_E2栏,如果你使用<代码>AND<>代码,你就表示,所有记录都必须遵守这两项限制。 在分开的询问中,他们只尊重他们,因此,在主问中必须如此。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签