开发者

How do you do a mysql join where the join may come from one or another table

This is the query that I am using to match up a members name to an id.

SELECT eve_member_list.`characterID` ,eve_member_list.`name` 
FROM `eve_mining_op_members`
INNER JOIN eve_member_list ON eve_mining_op_members.characterID = eve_member_list.characterID
WHERE op_id = '20110821105414-741653460';

My issue is that I have two different member lists, one lists are members that belong to our group and the second list i开发者_StackOverflows a list of members that do not belong to our group.

How do i write this query so that if a member is not found in the eve_member_list table it will look in the eve_nonmember_member_list table to match the eve_mining_op_members.characterID to the charName

I apologize in advance if the question is hard to read as I am not quite sure how to properly ask what it is that I am looking for.


Change your INNER JOIN to a LEFT JOIN and join with both the tables. Use IFNULL to select the name if it appears in the first table, but if it is NULL (because no match was found) then it will use the value found from the second table.

SELECT
    characterID,
    IFNULL(eve_member_list.name, eve_nonmember_member_list.charName) AS name
FROM eve_mining_op_members
LEFT JOIN eve_member_list USING (characterID)
LEFT JOIN eve_nonmember_member_list USING (characterID)
WHERE op_id = '20110821105414-741653460';

If you have control of the database design you should also consider if it is possible to redesign your database so that both members and non-members are stored in the same table. You could for example use a boolean to specify whether or not they are members. Or you could create a person table and have information that is only relevant to members stored in a separate memberinfo table with an nullable foreign key from the person table to the memberinfo table. This will make queries relating to both members and non-members easier to write and perform better.


You could try a left join on both tables, and then selecting the non-null results from the resulting query -

select * from
(select * from
  eve_mining_op_members as x
  left join eve_member_list  as y1 on x.characterID = y1.characterID
  left join eve_member_list2 as y2 on x.characterID = y2.characterID) as t
where t.name is not null

Or, you could try the same thing with a union and using inner join (assuming joined tables are the same):

select * from
(select * from eve_mining_op_members as x
 inner join eve_member_list  as y1 on x.characterID = y1.characterID
  UNION
 select * from eve_mining_op_members as x
 inner join eve_member_list2 as y2 on x.characterID = y2.characterID) as t

You can throw in your op_id condition where you see fit (sorry, I didn't really understand where it came from). Good luck!


You have several options but by

  • using a UNION between the eve_member_list and eve_nonmember_member_list table
  • and JOIN the results of this UNION with your original eve_mining_op_members table

you will get your required results.

SQL Statement

SELECT  lst.`characterID` 
        , lst.`name` 
FROM    `eve_mining_op_members` AS m
        INNER JOIN (
          SELECT  characterID
                  , name
          FROM    eve_member_list
          UNION ALL                  
          SELECT  characterID
                  , name
          FROM    eve_nonmember_member_list
        ) AS lst ON lst.characterID = m.characterID
WHERE op_id = '20110821105414-741653460';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜