Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

mysql - How to use partition in Laravel

I have a table of employees with their name, dept and salary. See here

I want to fetch 2 highest salary employees from every dept

This is how I achieved in MySql,

select dept, name, salary from ( 
        select dept, name, salary,
        row_number() over (
                partition by dept 
                order by salary desc
            ) rn
        from 
        employees as T
    )as T
    where T.rn < 2;

How would I write this with Laravel Eloquent or Query Builder (Without using raw)?

question from:https://stackoverflow.com/questions/66056691/how-to-use-partition-in-laravel

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...