从表中选择带有另一个表中的参数

时间:2018-07-04 08:07:36

标签: mysql

第一个表meter_values

from keras.models import Sequential
from keras.layers import LSTM, Dense

nb_frames = 10

model = Sequential()
model.add(LSTM(20, input_shape=(nb_frames, 36)))
model.add(Dense(1, activation='relu'))
model.compile('rmsprop', 'mse')
model.summary()

第二个餐桌时段

|id   |  somedata | date(YY-MM-DD)| status | 
|-----+-----------+---------------+--------|
|1    |  tets     | 20180628      |   6    |
|2    |  setd     | 20180627      |   6    |
|3    |  ewrw5    | 20180701      |   6    |
|4    |  6werww   | 20180730      |   6    |
|5    |  werqwe   | 20180803      |   6    |
|6    |  wrwerw   | 20171130      |   6    |

我需要获取count(meter_values),其中meter_values.status = 6并根据表period.begin和period.end按年份和日期分组
示例:

| year   |  begin   | end      |
|--------+----------+----------|
| 201807 | 20180626 | 20180704 |    
| 201808 | 20180730 | 20180803 |
| 201801 | 20171228 | 20180104 |
| 201712 | 20171129 | 20171205 |

我尝试此查询

| year   |  begin   | end      | count(meter_values) |
|--------+----------+----------+---------------------|
| 201807 | 20180626 | 20180704 |          3          |
| 201808 | 20180730 | 20180803 |          2          |
| 201801 | 20171228 | 20180104 |          0          |
| 201712 | 20171129 | 20171205 |          1          |

但是我得到所有记录的计数

select *
        from period, 
        (select count(meter_values.id) 
            from meter_values, period
                where meter_values.date>=period.begin 
                    and meter_values.date<=period.end 
                    and meter_values.status=6
                    and period.begin is not null
                    and period.end is not null) 
            as mv
        where period.begin is not null and period.end is not null;

1 个答案:

答案 0 :(得分:2)

您可以使用内部联接和计数

/profile