I have two tables
Table A
+-----+---------+---------+
| pid | name | country |
+-----+---------+---------+
| 1 | ABC | XXXXXXX |
| 2 | EFG | YYYYYYY |
| 3 | IJK | ZZZZZZZ |
| 4 | LMN | AAAAAAA |
| 5 | OPQ | BBBBBBB |
| 6 | RST | CCCCCCC |
+-----+---------+---------+
Table B
+-----+---------+
| id | pid |
+-----+---------+
| 5 | 5 |
| 4 | 1 |
| 3 | 2 |
| 2 | 5 |
| 1 | 2 |
+-----+---------+
I want the below output
+-----+---------+---------+
| pid | name | country |
+-----+---------+---------+
| 5 | OPQ | BBBBBBB |
| 1 | ABC | XXXXXXX |
| 2 | EFG | YYYYYYY |
+-----+---------+---------+
That is the output should get the latest entries of Table B id field and the data of table A should be shown on appearing sequence of pid from Table B
Try this:
SELECT DISTINCT A.PID, A.NAME, A.COUNTRY
FROM TABLE_A A
INNER JOIN TABLE_B B
ON A.PID = B.PID
ORDER BY A.PID
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With