简体   繁体   中英

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.

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, ie:

SELECT * FROM table1 AS t1
JOIN table2 AS t2
ON t1.tab1_id=t2.id
GROUP BY t1.tab1_id
;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM