0

可能重复:
从多个表中获取数据

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

我必须distinctinner join RFM ON RFM.RFMID=PV.RFMID.

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

我如何distinct在这一行中使用

INNER JOIN RFM ON RFM.RFMID = PV.RFMID
4

2 回答 2

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'
于 2012-07-27T13:14:04.813 回答
-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' 

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

于 2012-07-27T13:14:58.790 回答