doudeng9425 2011-08-31 23:29
浏览 75
已采纳

如何在内连接上进行连接?

I have this database:

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

and I found out how to do an inner join on it like this:

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)

but what if I want to join this table with another one based on id and return some values , like date,from the second table as well:

table2

    id  date

6055903 111111111
6173767 111111111
5611411 111111111

Any ideas on this?

edit:

the inner join is fine the way it is, i need to add the table2 on top of that query

  • 写回答

1条回答 默认 最新

  • dongli8979 2011-08-31 23:57
    关注

    Just add another JOIN at the end:

    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 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看