[英]mysql: query multiple records in 3 tables?
Good day fellow programmers. 各位程序员,大家好。 I have 3 tables, with following sample records.
我有3张桌子,下面是示例记录。
tbl_members has: tbl_members具有:
mem_id | mem_fname | mem_lname
1 | Ryan | Layos
2 | Dhave | Sebastian
3 | Staven | Siegal
4 | Ma Ethel | Yocop
5 | Kelvin | Salvador
6 | Herbert | Ares
tbl_member_status has: tbl_member_status具有:
status_id | mem_id | leader_id | process_id
1 | 2 | 1 | 2
2 | 3 | 5 | 3
3 | 4 | 6 | 4
4 | 5 | 1 | 4
5 | 1 | 6 | 4
(tbl_member_status.mem_id is foreign keyed to tbl_members.mem_id, and leader_id is also foreign keyed to tbl_members.mem_id because in my case a member can be a leader. 1 member 1 leader) (tbl_member_status.mem_id外键为tbl_members.mem_id,leader_id也外键为tbl_members.mem_id,因为在我的情况下,成员可以是领导者。1个成员1个领导者)
tbl_process has: tbl_process具有:
process_id | process_type
1 | CONSOLIDATION
2 | PRE-ENCOUNTER
3 | ENCOUNTER
4 | POST-ENCOUNTER
(a member has a process to take which i used enum with values: CONSOLIDATION, PRE-ENCOUNTER, ENCOUNTER, POST-ENCOUNTER, etc.) (成员有一个过程要使用我使用带有以下值的枚举:CONSOLIDATION,PRE-ENCOUNTER,ENCOUNTER,POST-ENCOUNTER等)
My question now is the proper sql query in getting the desired output query like this. 我的问题现在是正确的sql查询,以获取所需的输出查询,如下所示。
tbl_query_result tbl_query_result
mem_id | member_fname | member_lname | leader_fname | leader_lname | process_type
2 | dhave | sebastian | Ryan | Layos | PRE-ENCOUNTER
5 | Kelvin | Salvador | Ryan | Layos | POST-ENCOUNTER
do remember that two columns of tbl_member_status is referring to one column of tbl_members that is mem_id. 请记住,tbl_member_status的两列是指tbl_members的一列,即mem_id。
UPDATE: 更新:
what i have done so far: 到目前为止我做了什么:
SELECT member.mem_fname, member.mem_lname, leader.mem_fname, leader.mem_lname, tbl_process.process_type
FROM
tbl_member_status as mem_stats
INNER JOIN
tbl_members as member
INNER JOIN
tbl_members as leader
INNER JOIN
tbl_members ON mem_stats.member_id = member.mem_id
INNER JOIN
tbl_process ON tbl_process.process_id = mem_stats.process_id
WHERE
leader.mem_fname = 'Ryan'
This query gets all record even if the leader.mem_fname is not equal to 'Ryan' 即使leader.mem_fname不等于“ Ryan”,此查询也会获得所有记录
Because when you query. 因为当您查询。 The number of rows in the result matters.
结果中的行数很重要。 Like: if your result is for fname = ryan but then the match for mem.id in table memberstatus is two and then in process table is again two.
像:如果您的结果是fname = ryan,但是表memberstatus中的mem.id匹配项是2,然后在进程表中匹配项是2。 Inshort you will have 2 rows in final output.
简而言之,您将在最终输出中有2行。 Can you try this :
你可以尝试一下:
Select M.member_fname, M.mem_lname P.process_type from tbl_members M, tbl_member_status MS, tbl_process P where M.mem_id = MS.mem_id and MS.process_id = P.process_id and where M.member_fname = 'ryan'
Okay i misunderstood your question at first. 好吧,我一开始误解了你的问题。 I have a solution for you which will improve your database schema.
我为您提供了一个解决方案,它将改善您的数据库架构。 If one member has only one leader and a single leader has many memebers.
如果一个成员只有一个领导者,而一个领导者有许多成员。 Then why not create a different table called leader and connect to members table directly?
那么为什么不创建另一个表称为leader并直接连接到members表呢? So it will be a one to one relation.
因此,这将是一对一的关系。 Which will make querying much simpler.
这将使查询简单得多。 So now you have 4 tables.
因此,现在您有4张桌子。
Select M.member_fname, M.mem_lname, L.fname, L.lname, P.process_type
from tbl_members M, tbl_member_status MS, tbl_process P, tbl_leader L
where M.leader_id = L.id and M.mem_id = MS.mem_id and MS.process_id = P.process_id and where M.member_fname = 'ryan'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.