2011-08-31 33 views
1

我有這個數據庫:如何在內部連接上進行連接?

table

id fname  dphone  count_pic dup_id 

6055903 Karla  5126xxx798 1  57 
6173767 Aaliyah  4082xxx534 4  39 
5611411 Aaliyah  4082xxx534 15  39 
5611211 Aaliyah  4082xxx534 18  39 
4234798 Abby  3057xxx974 31  16 
6166691 Walter  6178xxx280 1  74 
3375576 Walter  6178xxx280 17  74 

,我發現怎麼做就可以了內部聯接是這樣的:

SELECT * 
    FROM table t1 
INNER JOIN (SELECT MIN(count_pic) AS minpic, 
       MAX(count_pic) AS maxpic, 
       dup_id 
      FROM table 
     GROUP BY dup_id) t2 ON t1.dup_id = t2.dup_id 
         AND (t1.count_pic = minpic 
          OR t1.count_pic = maxpic) 

,但如果我想加盟此表是什麼與另一個基於id並返回一些值,如第二個表中的date,以及:

table2

id date 

6055903 111111111 
6173767 111111111 
5611411 111111111 

對此的任何想法?

編輯:

內連接是好的事情是這樣的,我需要對查詢

+0

剛添加第二個連接? 'SELECT * FROM table1 JOIN table2 ON(table1.id = table2.foreign_id)JOIN table3 ON(table1.id = table3.foreign_id)' – Thorsten

回答

0

的頂部添加table2就在末尾加上另一JOIN

SELECT * 
    FROM table t1 
INNER JOIN (SELECT MIN(count_pic) AS minpic, 
       MAX(count_pic) AS maxpic, 
       dup_id 
      FROM table 
     GROUP BY dup_id) t2 ON t1.dup_id = t2.dup_id 
         AND (t1.count_pic = minpic 
          OR t1.count_pic = maxpic) 

INNER JOIN table2 ON t1.id = table2.id -- add this