5

i am only starting to play with MySQL and i seem to have been stumped by one problem.

I have one table containing a number of fields including a primary id field. I have a second table containing multiple rows with the value of user_id corresponding to the first table.

Or, this

Table 1:
Id name
1 foo
2 bar
3 baz
4 qux

Table 2:
id uid
1 1
2 2
3 2
4 2
5 3

I need to make a query with a limit i.e say LIMIT 3 Then the first 3 rows of table 1 should be selected, and all rows from table 2 having uid between 1 and 3 (The ids of the first 3 rows) should be returned.

2
  • Simple JOIN and a WHERE t2.uid IN(SELECT id FROM t1 ORDER BY id LIMIT 3)
    – Mihai
    Commented Apr 19, 2014 at 15:32
  • 1
    @Mihai: IN (... LIMIT x) is not allowed in MySQL. Commented Apr 19, 2014 at 17:58

1 Answer 1

5

You can use something like this:

select * from 
(select * from table1 limit 3) t1
join table2 t2 on t2.uid = t1.id

You can check it: http://sqlfiddle.com/#!2/1a534/7

1
  • So, If I apply any where condition for table 1, it will be applied after limit, right? Commented May 14, 2019 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.