Mysql 使用where从两列中选择单个值
对不起,如果这个问题是愚蠢或幼稚的,因为我是mysql新手 我的表格结构:Mysql 使用where从两列中选择单个值,mysql,Mysql,对不起,如果这个问题是愚蠢或幼稚的,因为我是mysql新手 我的表格结构: |------------|-------------|--------------| | SampleTime | ClosePrice | OpenPrice | |------------|-------------|--------------| | 09:30:00 | 2010.23 | 2010.83 | |------------|-------------|--------
|------------|-------------|--------------|
| SampleTime | ClosePrice | OpenPrice |
|------------|-------------|--------------|
| 09:30:00 | 2010.23 | 2010.83 |
|------------|-------------|--------------|
| 09:31:00 | 2009.53 | 2009.65 |
|------------|-------------|--------------|
| 09:32:00 | 2010.63 | 2010.66 |
|------------|-------------|--------------|
| 15:59:00 | 2010.76 | 2009.76 |
|------------|-------------|--------------|
我想要有OpenPrice,其中SampleTime为09:30:00,ClosePrice,其中SampleTime为15:59:00
我编写了一个查询,但它不起作用:
SELECT
OpenPrice,
ClosePrice
FROM Prices
WHERE SampleTime = '09:30:00'
and SampleTime ='15:59:00'
尝试在和之间使用
SELECT OpenPrice,ClosePrice FROM Prices WHERE SampleTime between '09:30:00' and '15:59:00'
或者从你的问题来看
select min(case when SampleTime='09:30:00' then openprice end) as openprice,
min(case when SampleTime='15:59:00' then closeprice end) as closeprice from prices
你能做一个工会吗
SELECT OpenPrice as Price FROM Prices WHERE SampleTime = '09:30:00'
union
Select ClosePrice as Price from Prices where SampleTime ='15:59:00';
这应该可以做到:
SELECT
MAX(case when SampleTime='09:30:00' then OpenPrice end) OpenPrice,
MAX(case when SampleTime='15:59:00' then ClosePrice end) ClosePrice
FROM Prices
使用下面的查询
SELECT a.OpenPrice, b.ClosePrice FROM Prices AS a, Prices AS b WHERE a.SampleTime='09:30:00' AND b.SampleTime='15:59:00'
要在一行中获取这些值,只需在自己的子查询中选择所需的每个单元格,如下所示:
SELECT
(SELECT OpenPrice FROM Prices WHERE SampleTime = '09:30:00') AS OpenPrice,
(SELECT ClosePrice FROM Prices WHERE SampleTime ='15:59:00') AS ClosePrice
另外,为了提高性能,您应该在
SampleTime
上设置一个索引。刚刚注意到这看起来像是@fa06答案的重复。看起来我们在同一时间有着相同的想法…但是我的答案在你之前8分钟就已经发布了..没关系..人们在同一时间有着相同的想法:)我打字很慢。。。尤其是在我的智能手机上这里有一些答案,人们或多或少都在猜测你想要什么样的输出。提供预期输出的示例总是有益的