4

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.

  • Simple JOIN and a WHERE t2.uid IN(SELECT id FROM t1 ORDER BY id LIMIT 3) – Mihai Apr 19 '14 at 15:32
  • @Mihai: IN (... LIMIT x) is not allowed in MySQL. – ypercubeᵀᴹ Apr 19 '14 at 17:58
4

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

|improve this answer|||||
  • So, If I apply any where condition for table 1, it will be applied after limit, right? – Akshit Arora May 14 '19 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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