简体   繁体   English

MySQL查询以基于列的方式联接表

[英]MySQL Query to join tables based on columns

Just looking for some help with this, i'm sure it is incredibly simple but after so many hours doing other areas of my site, i'm going a bit batty. 只是寻找一些帮助,我敢肯定这非常简单,但是经过很多小时在我网站的其他区域工作后,我有点不高兴了。

I just have a gaming competition whereby I have a table called 'leaders' that has only these columns: 我只是参加一场游戏比赛,因此我有一个名为“ leaders”的表,其中只有以下几列:

fk_memberid | fk_memberid | points_total points_total

Quite simple. 非常简单。 Then I have this query I found elsewhere on this forum to just get the rankings of each member. 然后,我在这个论坛的其他地方找到了这个查询,只是获得每个成员的排名。

    SELECT 
    fk_memberid, 
    points_total, 
    (SELECT COUNT(*)+1 FROM leaders WHERE points_total>x.points_total) AS rank_upper, 
    (SELECT COUNT(*) FROM leaders WHERE points_total>=x.points_total) AS rank_lower 
FROM 
    `leaders` x 

My question is, how do I link the fk_memberid column to another table called "members" to the corresponding column "k_memberid"? 我的问题是,如何将fk_memberid列链接到另一个名为“ members”的表,并将其链接到对应的列“ k_memberid”? I do this all the time of course but for some reason i'm struggling in this case due to the different type of query i'm familiar with above. 我当然会一直这样做,但是由于某种原因,由于我上面熟悉的查询类型不同,我在这种情况下仍然很挣扎。

Sorry for the likely incredibly easy answer. 对不起,这个答案很简单。 Appreciate the help. 感谢帮助。

Do one thing SELECT * FROM Table1 T1, Table2 T2 WHERE T1.column_name = T2.column_name AND Another_comditio(if you want); 做一件事SELECT * FROM Table1 T1,Table2 T2 WHERE T1.column_name = T2.column_name AND Another_comditio(如果需要); may be it will help you 可能会对您有帮助

A quick example here: 这里有个简单的例子:

SELECT l.fk_memberid, l.points_total, m.first_name FROM leaders l 
left join member m
on m.k_member_id=l.fk_member_id
WHERE ...

This will give you back a table with 3 columns, 2 from leader table and "first_name" (assuming it exists) from member table 这将为您提供一个包含3列的表,其中2个来自领导者表,另外2个来自成员表“ first_name”(假设它存在)

选择*从领导者LEFT JOIN成员成为members.k_memberid = Leaders.fk_memberid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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