Ich habe eine SQL-Abfrage geschrieben und versuche, es an Sequelize anzupassen, kann aber nicht herausfinden, wie Sie eine Zuordnung in der gleichen Tabelle erstellen. Hier ist die SQL:Inner Join am gleichen Tisch Sequelize
SELECT t1.*
FROM `configurations` t1
INNER JOIN
(
SELECT max(configuration_start) maxConfigStart, pb_id
FROM `configurations`
WHERE
active = 1
AND DATE(configuration_start) <= DATE(NOW())
GROUP BY `pb_id`
) t2
ON t1.`pb_id` = t2.`pb_id`
AND t1.`configuration_start` = t2.`maxConfigStart`
WHERE t1.`active` = 1
ORDER BY t1.`configuration_start` DESC
Hier ist die Bestimmung des Modells:
configurations = sequelize.define 'configurations', {
id:
type: Sequelize.INTEGER
primaryKey: true
autoIncrement: true
configuration_name: Sequelize.STRING
pb_id: Sequelize.INTEGER
device_id: Sequelize.STRING
event_id: Sequelize.INTEGER
configuration_start: Sequelize.DATE
configuration_end: Sequelize.DATE
utc_offset: Sequelize.STRING
watermark_id: Sequelize.INTEGER
ipad_config_id: Sequelize.INTEGER
screen_group_id: Sequelize.INTEGER
color_table_id: Sequelize.INTEGER
animated_overlay_group: Sequelize.INTEGER
created: Sequelize.DATE
updated: Sequelize.DATE
active: Sequelize.INTEGER
}, {
timestamps: false,
scopes: {
currentConfigTimes: {
attributes: ['pb_id', [sequelize.fn('max', sequelize.col('configuration_start')), 'configuration_start']]
where:
active: 1
configuration_start:
lte: new Date()
group: ['pb_id']
},
currentConfigIds: {
attributes: ['pb_id', 'configuration_start', 'id']
where:
active: 1
configuration_start:
lte: new Date()
order: [['configuration_start', 'DESC']]
}
}
}
Jede Hilfe, dass jemand bieten kann sehr geschätzt werden würde. Im Wesentlichen versuche ich, die letzte letzte Konfiguration für jede pb_id
zu erhalten. Vielleicht gibt es einen besseren Weg, es zu tun?
Sie SQL sieht kein Problem und Sie brauchen nicht die letzte WHERE .... Also, was ist dein Problem? – SIDU
Ich weiß nicht, wie man das in Sequelize macht. Ich kann es in SQL gut machen, weiß aber nicht, wie ich es in mein ORM übertrage. – tonestrike