Sequeilize find highest value of table in where clause

b0ss

I have a Table called Seasons where columns are "start_at" and "end_at". these columsn contain year values like 2019 and 2020 and so on. I want to update one row which has the highest value in "end_at" column. I tried something like this:

Season.update({is_current: 0}, {where: {is_current: 1}}))
.then(() => Season.update({is_current: 1}, {
    where: sequelize.where(
        sequelize.fn("MAX", sequelize.col("end_at"))
    )
}))

But it gives me an error:

Error: Invalid use of group function

Can somebody help me?

Vivek Doshi

You can do it like this :

Season.findOne({
    where : { is_current : 0 }, // find records which is_current = 0;
    order : [  ['end_at', 'DESC'] ] // order by desc will make highest to the first record
}).then(ref => {
    if(ref) { // <-- Check if any record exists
        ref.is_current = 1; //<--- Update Value in instance
        return ref.save(); // <-- Save to DB
    }

});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

MySQL Correct WHERE clause with a value pair table like Wordpress usermeta

array find the second highest value

Where clause on joined table

Get the highest id from a specific where clause

How to write a WHERE clause that uses the MIN() value of a column in the same the table?

Find the highest value in a dataframe

Check value from whole table that match where clause in sql

How to find nearest highest value when join data.table

Mysql: use join table column value in where clause

Find the highest number in a table

Find highest value in multidimensional array and keep highest

find element with highest value with XPath?

Using a subquery in the where clause to select 2nd highest date from a table

Javascript: Find variable with highest and second highest value

use nested table value in where clause(oracle)

selecting highest value in a table

SQL Get highest repeating value for a group clause

NULL value in where clause

jQuery/JavaScript find highest value in table column

Find the second highest value

SET value in the WHERE clause

Using a subquery in a where clause to find the second smallest value

Sql select from a table but use another table value as where clause

How to find same value in same table without hardcoding in where clause?

Is there a way to use a table value attribute in where clause in SQL

How to find the highest Value with Python

Get highest value from column in where clause

Rails scope where clause find nested array value in hash

Using second highest value in an ON clause