简体   繁体   中英

PHP mysqli. ORDER SELECT after one table but SELECT data from another table?

I have 2 tables.

visitors:

pageID
visitorID
last_visit

users:

userID(this ID and the pageID and visitorID in visitors is bundled to the same exact user)
username
age

I want to select last 5 visitors of the current logged in user ( ORDER BY last_visit LIMIT 5 in the table visits ) I want to select the data from table users of those 5 users.(username, age, etc.)

How do i do this?

Use a JOIN with a subquery that gets the last 5 visitors.

SELECT u.*
FROM users AS u
JOIN (SELECT visitorId
      FROM visitors AS v
      WHERE v.pageID = $currentUserID
      ORDER BY last_visit
      LIMIT 5) AS v1
ON u.userID = v1.visitorID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM