简体   繁体   中英

SQL Select top 3 rows with highest value in a certain column

Hi Stackoverflow users,

I'm currently working on a small website and I need a SQL query that selects 3 rows with the most 'Likes'. I've tried using max and top 3 but nothing seem to work for me. I would appreciate some help from you guys! Thanks in advance.

在此处输入图片说明

Using TOP won't work with MySQL, because that is SQL Server (or maybe Access) syntax. You probably want LIMIT here:

SELECT *
FROM yourTable
ORDER BY Likes DESC
LIMIT 3;

We could have also used:

LIMIT 3, OFFSET 0;  -- three records with no offset
LIMIT 0, 3          -- same as above

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