我想显示如下所示的3个表的列值(数据显示、温度、压力),我的查询也显示在下面,但是这里的温度值是重复的,我想要所有对应的特定日期的值,我该怎么做呢?有人能帮我吗?
表是
名称移位操作员id植物线机:00.000 Plant1 Line1 mc1 Pra深处shift1(7-3) Operator1 101 2011-06-06 00:00:00.000 Plant1 Line1 mc1 .
温度时间日期
温度时间日期00:00.
我的sql查询是
select Date=convert(varchar(12),(t1.date),101), t1.time, operatorname,
machine, line, t2.time, Temprature, Pressure
from datalogging5 t
inner join temprature t1 on t1.date=t.date
inner join pressure t2 on t.date=t2.date order by t.date发布于 2011-06-10 10:20:09
我在SQL方面不是很流利,所以如果我做错了什么,请告诉我,但是你不能这样做:
SELECT CONVERT(varchar(12),(t1.date),101) AS Date, t1.time, t.operatorname,
t.machine, t.line, t2.time, t1.temprature, t2.pressure
FROM datalogging5 t
INNER JOIN temprature t1 ON t1.date=t.date
INNER JOIN pressure t2 ON t.date=t2.date
WHERE t.date = '2011-6-10'
ORDER BY t.date;您可以用搜索查询的任何内容替换2011-6-10。至于温度重复问题,我认为问题在于您试图通过date列连接表。有可能不止一个数据记录或温度条目有相同的日期,所以当您运行您的查询时,它会对它匹配的每条记录显示一次,从而使它看起来像一个副本。
不管怎么说,让我知道这段代码是不是你想要的。
发布于 2011-06-10 10:39:38
您需要在日期和时间上加入温度和压力表(因为它们显然存储在单独的列中)。
Select D.Date, T.Time...
From DataLoggings As D
Join Temperature As T
On T.Date = D.Date
Join Pressure As P
On P.Date = T.Date
And P.Time = T.Time您的模式和这个查询有一些捕获:
你可以考虑一个测量表,而不是一个单独的温度和压力表。所以,就像:
Create Table Measurements
(
ReadingType varchar(15) not null
, Reading float not null
, Date date not null
, Time time not null
, Check( ReadingType In('Temperature','Pressure') )
)https://stackoverflow.com/questions/6304733
复制相似问题