Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

table 1 contains fields: tab1_id name, description

table 2 contains fields: tab2_id,id,choice.

by using the following query

  SELECT * FROM table1 AS t1
  INNER JOIN table2 AS t2
  ON t1.tab1_id=t2.id

it returned several rows of table2 for each t1.tab1_id.

What I want is to get only first row of table2 for each ta.tab1_id.

Please help, thanks.

share|improve this question

1 Answer

up vote 1 down vote accepted

Just use a GROUP BY, just make sure to include every column in your GROUP BY that you want to use in your SELECT clause, i.e.:

SELECT * FROM table1 AS t1
JOIN table2 AS t2
ON t1.tab1_id=t2.id
GROUP BY t1.tab1_id
;
share|improve this answer
 
Your answer is very simple and also my need. Thanks Ruben. –  AJ OP Jun 19 at 0:06
 
You're welcome! –  Ruben Jun 19 at 2:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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