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

985

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.

816

Answer

Solution:

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
;

People are also looking for solutions to the problem: php - Symfony2 Pager Fanta

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.