AJ OP
AJ OP

Reputation: 445

How to SELECT data from two tables which only first row of second table is selected for one row of first table

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.

Upvotes: 0

Views: 7543

Answers (1)

Ruben
Ruben

Reputation: 5095

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
;

Upvotes: 1

Related Questions