| |

VerySource

 Forgot password?
 Register
Search
View: 643|Reply: 4

Find a SQL statement about INNER JOIN

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-27 07:20:01
| Show all posts |Read mode
There is a table association structure as follows

a table

    id fid name time
    01 43 ee 2016-12-22
    02 41 qq 2016-12-22
    03 42 88 2016-12-21
b table
    id aid sj time
   guid 01 1111 2016-12-22
   guid 02 1111 2016-12-24
   guid 03 1111 2016-12-22
   guid 03 1111 2016-12-24
-------------------------------------------------- ----------------
Table a is used to store user information, and table b is a review of user information a.id = b.aid
When I only approve it once, using INNER JOIN to query everything works normally and I get the result I want!
However, after reviewing twice, more than one result is returned. Can I add a condition to the ON of the INNER JOIN to get the most relevant one to get a related data?
--------------------------------------------------
Master please help
Reply

Use magic Report

0

Threads

58

Posts

32.00

Credits

Newbie

Rank: 1

Credits
32.00

 China

Post time: 2020-2-19 20:15:01
| Show all posts
select a1. *
from a a1
inner join b b1 on a1.id = b1.aid
where b1.time = (select max (b2) from b b2 where b1.id = b2.id and b1.aid = b2.aid)
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-19 21:00:01
| Show all posts
select top 1 ......... order by b.time desc
Reply

Use magic Report

0

Threads

58

Posts

32.00

Credits

Newbie

Rank: 1

Credits
32.00

 China

Post time: 2020-2-19 21:15:01
| Show all posts
select a1. *
from a a1
inner join b b1 on a1.id = b1.aid
where b1.time = (select max (b2.time) from b b2 where b1.id = b2.id and b1.aid = b2.aid)
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-2-20 19:45:02
| Show all posts
Can you simply specify a condition in on to achieve this, although the effect can be achieved, is the efficiency too low?
Ha ha
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list