简体   繁体   English

在MySQL数据库中搜索多个表

[英]Searching multiple tables in MySQL database

I have several different tables in my database(mySQL). 我的数据库(mySQL)中有几个不同的表。

Here are the relevant coumns for the tables 这是表格的相关列

table_tournaments table_tournaments

  1. tournamentId 比赛编号
  2. tournamnetName tournamnetName
  3. tournamentStatus 比赛状态

table_tournament_results table_tournament_results

  1. tournamentId 比赛编号
  2. playerId playerId
  3. playerName 选手姓名
  4. playerRank playerRank
  5. tournamnetParticipants 参加者

table_players table_players

  1. playerId playerId
  2. playerName 选手姓名

The tournaments table contains the information about the tournament, the tournament results table shows the results from that table 比赛表包含有关比赛的信息,比赛结果表显示该表的结果

I want to search the tournaments table by name and then with the returned results get the information from the tournament results table. 我想按名称搜索比赛表,然后使用返回的结果从比赛结果表中获取信息。

    SELECT * FROM `tournaments` `WHERE` `tournamentName` LIKE "%Query%"

I'm not sure how to go about this, maybe I need to do something via PHP , any and all help is appreciated. 我不确定该怎么做,也许我需要通过PHP来完成某些工作,我们将不胜感激。

You can get the results you want with a join operation. 您可以通过联接操作获得所需的结果。

This is an example of an outer join , returning all rows from t that have the string 'foo' appearing as part of tournament_name , along with any matching rows from r . 这是一个外部联接的示例,它返回t中所有出现字符串'foo'行,它们都出现在tournament_name ,以及r所有匹配行。

A relationship between rows in the two tables is established by storing a common value in the tournamentId column of the two tables. 两个表中的行之间的关系是通过在两个表的tournamentId列中存储一个公共值来建立的。 The predicate in the ON clause specifies the condition that determines if a row "matches". ON子句中的谓词指定确定行是否“匹配”的条件。

 SELECT t.tournamentId
      , t.tournamentName
      , t.tournamentStatus
      , r.playerId
      , r.playerName
      , r.playerRank
   FROM table_tournaments t
   LEFT
   JOIN table_tournament_results r
     ON r.tournamentId = t.tournamentId
  WHERE t.tournament_name LIKE '%foo%'
  ORDER
     BY t.tournamentId
      , r.playerId

The t and r that appear after the table names are table aliases , we can qualify references to the columns in each table by prefacing the column name with the table alias and a dot. 表名后面出现的tr是表别名 ,我们可以通过在表名前面加上表别名和点来限定对每个表中列的引用。 This makes the column reference unambiguous. 这使列引用明确。 (In the case of tournamentId , MySQL doesn't know if you are referring to the column in t or r , so we qualify it to make it explicit. We follow this same pattern for all column references. Then, someone reading the statement doesn't need to wonder which table contains the column playerId . (就tournamentId ,MySQL不知道您是在t还是r中引用该列,因此我们对其进行限定以使其明确。我们对所有列引用遵循相同的模式。然后,读取该语句的人不会无需怀疑哪个表包含列playerId

Your Query may be like this 您的查询可能是这样的

SELECT a.*, b.tournamnetName FROM table_tournament_results a 
left join table_tournaments on a.tournamentId=b.tournamentId
WHERE b.tournamnetName LIKE "%Query%"

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

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