简体   繁体   中英

SQL Query to select multiple rows with ID

I need to select all rows from a table (see structure below) but that's more complex.

I want 5 unique idm but all rows with this idm (which is the ID of a conversation).. How can I do it ?

+------------+
| messagerie |
+------------+
|     ID     |
|    idm     |
|    send    | 
|   receipt  |
|   subject  |
|  message   |
+------------+
  • Select distinct idms for uniqueness and use LIMIT keyword to select only 5 .
  • Use inner join clause to select all rows who have this idm .
  • Below query would give you 5 unique idms at random all the time.

SQL:

select *
from messagerie m1
inner join (select distinct idm
              from messagerie
              order by rand() LIMIT 5) m2
on m1.idm = m2.idm;

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