У меня проблема с запросом sequelize, я думаю, что они должны дать тот же результат, но это не так. Может кто-нибудь объяснить эту ситуацию?
Первый случай:
getLatestStation = (stationId) => {
return models.MonitoringDataInfo.findAll({
where: {stationId :stationId},
attributes: ['id', 'stationId', 'sentAt'],
order : [['sentAt', 'DESC']],
limit: 1,
include : [{model: models.MonitoringData, attributes: ['idData', 'indicator', 'value', 'unit', 'sensorStatus']}]
})
}
Первый результат:
Первый запрос:
SELECT [MonitoringDataInfo]. *, [MonitoringData]. [Id] AS [MonitoringData.id], [MonitoringData]. [IdData] AS [MonitoringData.idData], [MonitoringData]. [Индикатор] AS [MonitoringData.indicator], [MonitoringData]. [Значение] AS [MonitoringData.value], [MonitoringData]. [Единица] AS [MonitoringData.unit], [MonitoringData]. [SensorStatus] AS [MonitoringData.sensorStatus] FROM (ВЫБРАТЬ [MonitoringDataInfo]. [Id], [MonitoringDataInfo]. [StationId], [MonitoringDataInfo]. [SentAt] FROM [Monitoring_data_info] AS [MonitoringDataInfo] ГДЕ [MonitoringDataInfo]. [StationId] = N'mkkneh3uBrAkw9hepMlF 'ORDER BY [MonitoringDataInfo]. [SentAt] DES C СМЕЩЕНИЕ 0 СТРОК ПОЛУЧИТЬ СЛЕДУЮЩАЯ 1 СТРОКА ТОЛЬКО КАК [МониторингДанных_данных_Отд]] MonitoringData] ON [MonitoringDataInfo]. [Id] = [Monito ringData]. [idData] ORDER BY [MonitoringDataInfo]. [sentAt] DESC;
Второй случай:
getLatestStation = (stationId) => {
return models.Station.findAll({
attributes: ['id', 'name', 'address', 'envIndex'],
where : {
id : stationId,
publicStatus : 1
},
include : [{
model: models.MonitoringDataInfo,
limit: 1,
// where : {
// stationId: stationId
// },
attributes: ['id', 'stationId', 'sentAt'],
order : [['sentAt', 'DESC']],
limit : 1,
include : [{model: models.MonitoringData, attributes: ['idData', 'indicator', 'value', 'unit', 'sensorStatus']}]
}]
})
}
Второй результат:
Второй запрос: SELECT [Station]. [Id], [Station]. [Name], [Station]. [Address], [Station]. [EnvIndex] FROM [ станции] КАК [Станция] ГДЕ [Станция]. [id] = N'mkkneh3uBrAkw9hepMlF 'AND [Станция]. [publicStatus] = 1;
ВЫБЕРИТЕ [MonitoringDataInfo]. [Id], [MonitoringDataInfo]. [StationId], [MonitoringDataInfo]. [SentAt], [MonitoringData]. [Id] AS [MonitoringData.id], [MonitoringData]. [IdData] AS [MonitoringData.idData], [MonitoringData]. [Индикатор] AS [MonitoringData.indicator], [MonitoringData]. [Значение] AS [MonitoringData.value], [MonitoringData]. [Единица измерения] AS [MonitoringData.unit], [MonitoringData ]. [sensorStatus] AS [MonitoringData.sensorStatus] ОТ [Monitoring_data_info] AS [MonitoringDataInfo] ВЛЕВО НАРУЖНОЕ СОЕДИНЕНИЕ [мониторинг_данных] AS [MonitoringData] ВКЛ [MonitoringDataInfo]. [id] = [MonitoringData]. [idData] ГДЕ [MonitoringDataInfo]. [stationId] IN (N'mkkneh3uBrAkw9hepMlF ') ЗАКАЗАТЬ ПО [MonitoringDataInfo]. [sentAt] DES C, [MonitoringDataInfo]. [id] OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY;