简体   繁体   中英

How to use limit and group by together in mysql

With these sample tables

table_a               table_b
 column_1               column_1 column_2
  1                         1       A
  2                         1       B
  3                         2       C
  4                         3       D
  5                         4       E

the query below

SELECT table_a.column1,table_b.column2 
FROM table_a 
INNER JOIN table_b ON table_a.column1 = table_b.column_1
GROUP BY table_a.column1 LIMIT 3

gives only 2 results (limit is 3) since the value 1 is duplicating in table_b. How can i get 3 results with unique table_a.column1 values. In general how can i use group by and limit together with group by having no impact on the limit

After minor adjustments with columns names (changing column1 into column_1 and column2 into column_2 ) your query gives me exactly 3 rows of results.

1 A
2 C
3 D

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