使用内部联接在三个表

时间:2017-04-04 11:05:33

标签: sql inner-join

我正在尝试获取属于活跃玩家的头像以及电子邮件以“a”开头的玩家的名称。

SELECT NAME 
FROM AVATAR 
  INNER JOIN PLAYERAVATAR ON PLAYERAVATAR.PLAYER_ID = PLAYERAVATAR.AVATAR_ID 
  INNER JOIN PLAYER ON PLAYER.ACTIVE = 1;

我有这个,但它不起作用。我并不擅长这个,所以任何帮助都会受到赞赏。谢谢。

+-----------+--------------------+-----+--+---+--+
|  AVATAR   |                    |     |  |   |  |
+-----------+--------------------+-----+--+---+--+
| AVATAR_ID | NUMBER(38,0)       | No  |  | 1 |  |
| NAME      | VARCHAR2(500 BYTE) | Yes |  | 2 |  |
| DOB       | VARCHAR2(500 BYTE) | Yes |  | 3 |  |
| HOARD     | NUMBER(38,0)       | Yes |  | 4 |  |
| STRENGH   | NUMBER             | Yes |  | 5 |  |
| GENDER    | VARCHAR2(500 BYTE) | Yes |  | 6 |  |
| SPECIES   | VARCHAR2(500 BYTE) | Yes |  | 7 |  |
+-----------+--------------------+-----+--+---+--+

+-----------+--------------------+-----+--+---+--+
|  PLAYER   |                    |     |  |   |  |
+-----------+--------------------+-----+--+---+--+
| PLAYER_ID | NUMBER(38,0)       | No  |  | 1 |  |
| NAME      | VARCHAR2(500 BYTE) | Yes |  | 2 |  |
| EMAIL     | VARCHAR2(500 BYTE) | Yes |  | 3 |  |
| ACTIVE    | NUMBER(38,0)       | Yes |  | 4 |  |
| PASSWORD  | VARCHAR2(500 BYTE) | Yes |  | 5 |  |
+-----------+--------------------+-----+--+---+--+
+--------------+--------------+-----+--+---+--+
| PLAYERAVATAR |              |     |  |   |  |
+--------------+--------------+-----+--+---+--+
| PLAYER_ID    | NUMBER(38,0) | Yes |  | 1 |  |
| AVATAR_ID    | NUMBER(38,0) | Yes |  | 2 |  |
+--------------+--------------+-----+--+---+--+

4 个答案:

答案 0 :(得分:0)

您可以这样做:

DISTINCT

如果您想选择唯一头像名称,可以使用SELECT DISTINCT AVATAR.NAME FROM AVATAR INNER JOIN PLAYERAVATAR ON PLAYERAVATAR.AVATAR_ID = AVATAR.AVATAR_ID INNER JOIN PLAYER ON PLAYER.PLAYER_ID = PLAYERAVATAR.PLAYER_ID WHERE PLAYER.ACTIVE = 1 AND PLAYER.EMAIL LIKE 'a%' ; ,如下所示:

public class SignUpUser {
    public var fullName : String?
    public var id : Int?
    public var city : String?
    public var email : String?
    public var address : String?
    public var lastName : String?
    public var countryCode : String?
    public var firstName : String?
    public var zipCode : Int?
    public var contactNumber : Int?
    public var sex : String?
    public var dob : String?
    public var signupType : String?
    public var verified : String?
    public var emailTokenExpiration : String?
    public var updatedAt : String?
    public var createdAt : String?

/**
    Returns an array of models based on given dictionary.

    Sample usage:
    let user_list = User.modelsFromDictionaryArray(someDictionaryArrayFromJSON)

    - parameter array:  NSArray from JSON dictionary.

    - returns: Array of User Instances.
*/
    public class func modelsFromDictionaryArray(array:NSArray) -> [SignUpUser]
    {
        var models:[SignUpUser] = []
        for item in array
        {
            models.append(SignUpUser(dictionary: item as! NSDictionary)!)
        }
        return models
    }

/**
    Constructs the object based on the given dictionary.

    Sample usage:
    let user = User(someDictionaryFromJSON)

    - parameter dictionary:  NSDictionary from JSON.

    - returns: User Instance.

*/

    init?() {}


    required public init?(dictionary: NSDictionary) {
        fullName = dictionary["fullName"] as? String
        id = dictionary["id"] as? Int
        city = dictionary["city"] as? String
        email = dictionary["email"] as? String
        address = dictionary["address"] as? String
        lastName = dictionary["lastName"] as? String
        countryCode = dictionary["countryCode"] as? String
        firstName = dictionary["firstName"] as? String
        zipCode = dictionary["zipCode"] as? Int
        contactNumber = dictionary["contactNumber"] as? Int
        sex = dictionary["sex"] as? String
        dob = dictionary["dob"] as? String
        signupType = dictionary["signupType"] as? String
        verified = dictionary["verified"] as? String
        emailTokenExpiration = dictionary["emailTokenExpiration"] as? String
        updatedAt = dictionary["updatedAt"] as? String
        createdAt = dictionary["createdAt"] as? String
    }


/**
    Returns the dictionary representation for the current instance.

    - returns: NSDictionary.
*/
    public func dictionaryRepresentation() -> NSDictionary {

        let dictionary = NSMutableDictionary()

        dictionary.setValue(self.fullName, forKey: "fullName")
        dictionary.setValue(self.id, forKey: "id")
        dictionary.setValue(self.city, forKey: "city")
        dictionary.setValue(self.email, forKey: "email")
        dictionary.setValue(self.address, forKey: "address")
        dictionary.setValue(self.lastName, forKey: "lastName")
        dictionary.setValue(self.countryCode, forKey: "countryCode")
        dictionary.setValue(self.firstName, forKey: "firstName")
        dictionary.setValue(self.zipCode, forKey: "zipCode")
        dictionary.setValue(self.contactNumber, forKey: "contactNumber")
        dictionary.setValue(self.sex, forKey: "sex")
        dictionary.setValue(self.dob, forKey: "dob")
        dictionary.setValue(self.signupType, forKey: "signupType")
        dictionary.setValue(self.verified, forKey: "verified")
        dictionary.setValue(self.emailTokenExpiration, forKey: "emailTokenExpiration")
        dictionary.setValue(self.updatedAt, forKey: "updatedAt")
        dictionary.setValue(self.createdAt, forKey: "createdAt")

        return dictionary
    }

}

答案 1 :(得分:0)

您有错误,因为列name位于多个表中。

您应该使用别名。

试试这个:

SELECT 
    P.NAME 
FROM 
    AVATAR A
INNER JOIN 
    PLAYERAVATAR PL ON PL.PLAYER_ID = A.PLAYER_ID
INNER JOIN 
    PLAYER P ON P.ACTIVE = 1 AND P.PLAYER_ID  = A.PLAYER_ID
WHERE 
    SUBSTR(P.EMAIL, 1, 1) = 'a'

答案 2 :(得分:0)

你可以使用

select a.NAME       from AVATAR   a
    join PLAYERAVATAR  pa on a.AVATAR_ID =pa.AVATAR_ID 
    join PLAYER   p on pa.PLAYER_ID    =p.PLAYER_ID   
    where p.ACTIVE = 1 and p.EMAIL     like 'a%'

如果您需要不同的头像名称,可以使用

 select distinct(a.NAME)

答案 3 :(得分:0)

从您请求的评论部分中的简短讨论中可以清楚地看出,您的数据模型实际上并不符合要求。你说一个化身只属于一个玩家。因此,将player_id存储在头像记录中并删除桥接表。

+-----------+--------------------+-----+--+---+--+
|  PLAYER   |                    |     |  |   |  |
+-----------+--------------------+-----+--+---+--+
| PLAYER_ID | NUMBER(38,0)       | No  |  | 1 |  |
| NAME      | VARCHAR2(500 BYTE) | Yes |  | 2 |  |
| ...       |                    |     |  |   |  |
+-----------+--------------------+-----+--+---+--+

+-----------+--------------------+-----+--+---+--+
|  AVATAR   |                    |     |  |   |  |
+-----------+--------------------+-----+--+---+--+
| AVATAR_ID | NUMBER(38,0)       | No  |  | 1 |  |
| NAME      | VARCHAR2(500 BYTE) | Yes |  | 2 |  |
| PLAYER_ID | NUMBER(38,0)       | No  |  | 3 |  |
| ...       |                    |     |  |   |  |
+-----------+--------------------+-----+--+---+--+

查询变为:

select name
from avatar
where player_id in
(
  select player_id
  from player
  where active = 1
  and email like 'a%'
);