简体   繁体   English

如何从涉及两个表的查询中获取单行。 子表的一项中重复使用2个外键

[英]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.就可以在此之后添加WHEREORDER子句以满足您的进一步需求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.

相关问题 MySQL查询根据来自两个不同表的值选择一个表。 - MySQL query to select one table based on values from two different tables. MySQL:“如何将行从一个表(链接)存储到多个(类别)表。” - MySQL : “How to store row from one table(link) to multiple(cateogry) tables.” 我需要更新连接两个不同表的表。 此查询合适吗? - I need to update a table joining two different tables. Is this query the appropriate one? 使用Eloquent从具有外键的单个表中获取数据到另外两个表-Laravel - Using Eloquent to fetch data from a single table with foreign keys to two other tables - laravel 如何获取最后插入的其他两个表的唯一组合外键作为单键 - how to get last inserted unique combined foreign keys of two other tables as single key 从同一行中的一个表中的单个查询中获取两个人的名字 - get two person names in a single query from the same table in one row mysql表有两个来自另一个表的外键 - mysql tables have two foreign keys from another same table 从两个表中的任何一个中获取单行 - Get single row from either of two tables 如何在单个查询中使用两个外键? - How use two Foreign Keys inside Single query? 我有2个MySql表。 如何在1 sql查询中获取不是所有数据? - I have 2 MySql tables. How to get not all data from them in 1 sql query?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM