简体   繁体   中英

Get data using inner join on three tables

I am trying to get the name of avatars which belong to active players and also the players whose email starts with "a".

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

I have this but it doesn't work. I'm not really good at this so any help would be appreciated. Thank you.

+-----------+--------------------+-----+--+---+--+
|  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 |  |
+--------------+--------------+-----+--+---+--+

You can do something like this :

SELECT 
    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%' ;

In case you want to select Unique Avatar Names , you can use DISTINCT , something like this :

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%' ;

You have error because column name is in more than one table.

You should use an alias.

Try this:

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'

you can use

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%'

if you want distinct avatar name you can use

 select distinct(a.NAME)

From our brief discussion in your request's comment section it becomes clear that your data model doesn't actually match the requirements. You say that an avatar belongs to only one player. So store the player_id in the avatar record and drop the bridge table.

+-----------+--------------------+-----+--+---+--+
|  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 |  |
| ...       |                    |     |  |   |  |
+-----------+--------------------+-----+--+---+--+

The query becomes:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM