使用Sql为指定的user_id进行自定义数据库查询

时间:2011-07-22 10:15:19

标签: sql database

我有这张桌子:

Master
 id
 some-fields

Detail
 detail_id (counter)
 master_id 
 user_id (related to another tables) 

我想仅为指定的user_id显示master的所有单个记录。 我知道的唯一方法......为每个细节增加一条线......

谢谢

2 个答案:

答案 0 :(得分:0)

SELECT DISTINCT x.* 
FROM Master x 
INNER JOIN Detail y ON x.id=y.master_id 
WHERE y.user_id=?

答案 1 :(得分:0)

select
      Master.*
   from
      ( select distinct Dtl.Master_ID
           from Detail Dtl
           where Dtl.User_ID = TheIDYouWant ) PreQuery
      join Master 
         on PreQuery.Master_ID = Master.ID