Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select unique rows sorted by id

Tags:

sql

sql-server

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

like image 532
Shubha Avatar asked Feb 12 '26 18:02

Shubha


1 Answers

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
like image 104
J Weezy Avatar answered Feb 15 '26 08:02

J Weezy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!