使用where从两列中选择一个值

时间:2018-09-05 05:04:52

标签: mysql

对不起,如果这个问题是愚蠢的或天真的,因为我是mysql的新手。

我的表结构:

|------------|-------------|--------------|
| 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 |
|------------|-------------|--------------|

我想拥有SampleTime为09:30:00的OpenPrice和SampleTime为15:59:00的ClosePrice。

我写了一个查询,但是没有用:

SELECT 
    OpenPrice,
    ClosePrice 
FROM Prices 
WHERE SampleTime = '09:30:00' 
    and SampleTime ='15:59:00'

5 个答案:

答案 0 :(得分:0)

尝试在&和

之间使用
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

答案 1 :(得分:0)

你能做一个工会吗?

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private FirebaseAuth mAuth = FirebaseAuth.getInstance();

private String type;
SharedPreferences sharedPref;

@Override
public void onTokenRefresh() {
    super.onTokenRefresh();
    if(mAuth.getCurrentUser() != null)
    {

        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);
        sendRegistrationToServer(refreshedToken);
    }
}

public void sendRegistrationToServer(String refreshedToken)
{
    sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    type = sharedPref.getString("Type", "");
    FirebaseDatabase.getInstance().getReference().child("Users").child(type).child(mAuth.getCurrentUser().getUid())
            .child("tokenID").setValue(refreshedToken);
}
}

答案 2 :(得分:0)

这应该可以完成工作:

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 

答案 3 :(得分:0)

在下面的查询中使用

SELECT a.OpenPrice, b.ClosePrice FROM Prices AS a, Prices AS b WHERE a.SampleTime='09:30:00' AND b.SampleTime='15:59:00'

答案 4 :(得分:0)

要在一行中获取这些信息,您只需在自己的子查询中选择所需的每个单元格,如下所示:

SELECT
    (SELECT OpenPrice FROM Prices WHERE SampleTime = '09:30:00') AS OpenPrice,
    (SELECT ClosePrice FROM Prices WHERE SampleTime ='15:59:00') AS ClosePrice

此外,出于性能考虑,您绝对应该在SampleTime上有一个索引。