[英]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. 这是我用来将成员名称与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 is 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
我如何写这个查询,这样,如果一个成员没有在发现eve_member_list
表会看在eve_nonmember_member_list
表的匹配eve_mining_op_members.characterID
到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. 将您的INNER JOIN
更改为LEFT JOIN
并加入两个表。 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. 如果名称出现在第一个表中,请使用IFNULL
选择名称,但是如果名称为NULL
(因为未找到匹配项),则它将使用从第二个表中找到的值。
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. 或者,您可以创建一个person
表,并具有仅与存储在单独的memberinfo
表中的成员相关的信息,并具有从person
表到memberinfo
表的可空外键。 This will make queries relating to both members and non-members easier to write and perform better. 这将使与成员和非成员有关的查询更易于编写且性能更好。
You have several options but by 您有几种选择,但是
UNION
between the eve_member_list
and eve_nonmember_member_list
table 在eve_member_list
和eve_nonmember_member_list
表之间使用UNION
JOIN
the results of this UNION
with your original eve_mining_op_members
table 并使用您原来的eve_mining_op_members
表JOIN
该UNION
的结果 you will get your required results. 您将获得所需的结果。
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';
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). 您可以在您认为合适的地方op_id
条件(对不起,我不太了解它的来源)。 Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.