[英]How to get single row from a query involving two tables. 2 Foreign Keys are repeated in one entry of child table
I have two tables 我有两张桌子
1. tbl_clubs
2. tbl_match_schedule
tbl_club has fields; tbl_club有字段;
fld_id | fld_club_name
tbl_match_schedule has fields; tbl_match_schedule具有字段;
fld_id | fld_club_id_one | fld_club_id_two | fld_match_time.
Now the flow is that admin will 1. go to the add new match schedule. 现在的流程是,管理员将1.转到添加新的比赛时间表。 2. Select the name of club from drop down 1 3. Select the name of the 2nd club (playing against) from drop down 2 4. Give Match time and save.
2.从下拉菜单1中选择俱乐部的名称。3.从下拉菜单2中选择第二个俱乐部(对阵)的名称。4.给比赛时间并保存。
Now the problem is that I am unable to select the data properly. 现在的问题是我无法正确选择数据。 I need one row, but it gives me two rows when I put the following query
我需要一行,但是当我输入以下查询时,它会给我两行
SELECT ms.fld_id, ms.fld_id_club_one , ms.fld_id_club_two , ms.fld_match_time, c.fld_club_name, c.fld_id FROM tbl_match_schedule as ms, tbl_club as c WHERE c.fld_id = ms.fld_id_club_one OR c.fld_id = ms.fld_id_club_two 从tbl_match_schedule中将ms.fld_id,ms.fld_id_club_one,ms.fld_id_club_two,ms.fld_match_time,c.fld_club_name,c.fld_id选择为ms,从tbl_club作为c WHERE c.fld_id = ms.fld_id_club_one = c.fld_id_club_one或c。
Please help me, I want something in this form "club name one"
vs "club name two"
at "19:00"
请帮助我,我想要这样的形式:
"19:00"
与"club name two"
"club name one"
对"club name two"
"19:00"
I am using mysql 我正在使用mysql
If i understand correctly this should do: 如果我正确理解,应该这样做:
SELECT c1.fld_club_name AS 'club_name_one', c2.fld_club_name AS 'club_name_two', s.fld_match_time AS 'at' FROM tbl_match_schedule s
LEFT JOIN tbl_clubs c1 ON s.fld_club_id_one = c1.fld_id
LEFT JOIN tbl_clubs c2 ON s.fld_club_id_two = c2.fld_id
Of course you can add a WHERE
or ORDER
clause after this to suit your further needs as long as you use the c1.
当然,只要您使用
c1.
就可以在此之后添加WHERE
或ORDER
子句以满足您的进一步需求c1.
and c2.
和
c2.
shorthands when you refer to the clubs as the full table name would be ambiguous. 当您提及俱乐部时,速记员的全名将是模棱两可的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.