Muhammad Hamid Muhammad Hamid - 1 year ago 70
MySQL Question

MySql Query to select 6 employees whose salaries sum upto total of $50,000

I know mysql but i dont know how this type of query will be executed.
I need a query that can select 6 employees whose salaries sum upto a total of $50,000.

Thanks in advance..!!

Answer Source

It was possible by retrieving all the records from the database and on the same time adding their salaries, when the rows number was 6 sum was equal to 50,000, then skip all the rows. This way i got 6 records whose sum of salaries was equal or less then 50,000. Code given below:

$query = mysqli_query($conn, "SELECT * FROM mma_player ORDER BY my_proj DESC"); while($row = mysqli_fetch_array($query)){

                        $salary = $row['salary']; 
                        $a = $salary + $salary_count;

                    if($row_count > 4 && $a > 50000){
                    if($row_count > 6){
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download