0

I am creating a Current Salary Report

This is my query:

$sql = "SELECT *
FROM payroll, employees
WHERE employees.username='$username'
AND payroll.id=employees.id";

And the output is: output and by scrolling down, this is the continuation of the output.

To sum it up, the query displays all the records of a certain ID but I would want it to display the last inserted data of this certain ID in the database. How can this happen?

theB
  • 6,450
  • 1
  • 28
  • 38
D. Gatch
  • 167
  • 4
  • 13

4 Answers4

1

To get the latest row you must order by id desc and to get only one record set limit 1.

So the code will be

$sql = "SELECT *
FROM payroll, employees
WHERE employees.username='$username'
AND payroll.id=employees.id ORDER BY employees.id DESC LIMIT 1 ";
h0ch5tr4355
  • 2,092
  • 4
  • 28
  • 51
0

try this

$sql = "SELECT *
FROM payroll, employees
WHERE employees.username='$username'
AND payroll.id=employees.id ORDER BY employees.id DESC  LIMIT 1 " 

if the newest ID is the largest value.

uno
  • 867
  • 2
  • 14
  • 29
0

Use this MySQL ORDER BY or LIMIT

$sql = "SELECT *
FROM payroll, employees
WHERE employees.username='$username'
AND payroll.id=employees.id ORDER BY employees.id DESC LIMIT 1";
Abhishek Sharma
  • 6,689
  • 1
  • 14
  • 20
0
$sql = "SELECT t1.*,t1.*
FROM payroll t1 join employees t2 on t1.id = t2.id
WHERE t1.username='$username' order by t2.id desc limit 1";
ashish mulani
  • 197
  • 1
  • 14