如何在RFM.RFMID = PV.RFMID上使用不同的INNER JOIN RFM

时间:2012-07-27 13:10:54

标签: sql sql-server

  

可能重复:
  Get data from multiple table

SELECT PM.PMID,RFM.TITLE,RFM.NAME ,PV.RFMID FROM MMASTER 
INNER JOIN AMM ON MMASTER.MID=AMM.MID
INNER JOIN PS ON AMM.AMMID=PS.AMMID
INNER JOIN PV ON PV.VTID=PS.VTID
INNER JOIN RFM ON RFM.RFMID=PV.RFMID
INNER JOIN PM ON PV.PMID==PM.PMID
INNER JOIN SM ON PS.SMID=SM.SMID
WHERE PM.PMID='2'

Table MMASTER
   MID col1
   1   abc
   2   xyx
   3   pqr

   Table AMM
   AMMID MID col1 col2
   1     1   bnb  mfk
   2     1   def  rwr
   3     2   re   wrwr

   Table PS
   UID VTID AMMID SMID col1 col2 
   1   1    1     1    rkk  jdj
   2   2    3     3    kdf  lfl
   6   2    2     4    rgr  rtr

   Table PV
   VTID PMID RFMID
   1    2   1
   2    2   3
   7    2   2

   Table RFM
   RFMID title name
   1     mr   john
   2     mr   jack
   3     mr   jim

   Table PM
   PMID col1 col2
   1   df  ere
   2   rwe rwer
   3   rwr fwr

   Table SM
   SMID MMID col1 col2
   1    1    fdf  efe
   2    1    ddf  dfdf
   3    2    df   ef

我在查询之后得到这样的结果

 PMID title name  PV.RFMID
 2     mr.   jim  3
 2     mr.   jim  3
 2     mr.   jim  3

我必须在distinct中使用inner join RFM ON RFM.RFMID=PV.RFMID

在我的查询中PV.RFMID在与RFM加入时获得重复值并且由于该标题和名称重复

如何在此行中使用distinct

INNER JOIN RFM ON RFM.RFMID = PV.RFMID

2 个答案:

答案 0 :(得分:3)

您可以使用INNER JOIN制作子查询并使用DISTINCT

SELECT MID,NAME,SEX,AGE,RFM.TITLE,RFM.NAME ,PV.RFMID
FROM MMASTER 
INNER JOIN AMM ON MMASTER.MID=AMM.MID
INNER JOIN PS ON AMM.AMMID=PS.AMMID
INNER JOIN PV ON PV.VTID=PS.VTID
INNER JOIN
(
    SELECT DISTINCT RFMID, NAME
    FROM RFM 
) RFM 
    ON RFM.RFMID=PV.RFMID
INNER JOIN PM ON PV.PMID==PM.PMID
INNER JOIN SM ON PS.SMID=SM.SMID
WHERE PM.PMID='13'

答案 1 :(得分:-1)

选择后使用distinct。无需在内部连接线使用..

 SELECT DISTINCT MID,NAME,SEX,AGE,RFM.TITLE,RFM.NAME 
 FROM MMASTER  
      INNER JOIN AMM ON MMASTER.MID=AMM.MID 
      INNER JOIN PS ON AMM.AMMID=PS.AMMID 
      INNER JOIN PV ON PV.VTID=PS.VTID 
      INNER JOIN RFM ON RFM.RFMID=PV.RFMID 
      INNER JOIN PM ON PV.PMID==PM.PMID 
      INNER JOIN SM ON PS.SMID=SM.SMID 
 WHERE PM.PMID='13' 

如果您想要所有唯一数据。