0

I have several different tables in my database(mySQL).

Here are the relevant coumns for the tables

table_tournaments

  1. tournamentId
  2. tournamnetName
  3. tournamentStatus

table_tournament_results

  1. tournamentId
  2. playerId
  3. playerName
  4. playerRank
  5. tournamnetParticipants

table_players

  1. 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.

2
  • tournamnetParticipants <- what's that? Commented May 13, 2015 at 6:06
  • It is a count of how many people participated in the tournament. Commented May 13, 2015 at 18:43

2 Answers 2

2

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.

A relationship between rows in the two tables is established by storing a common value in the tournamentId column of the two tables. The predicate in the ON clause specifies the condition that determines if a row "matches".

 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. 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.

Sign up to request clarification or add additional context in comments.

2 Comments

What does the LEFT argument or clause do?
The LEFT JOIN specifies an outer join operation. It says that we want to return all rows from the table on the left side (t), along with any matching rows from the table on the right side (r). If there isn't a matching row in r, the row from t (the left side) is returned, but with NULL values in place of the columns from r. If every row returned from t has at least one matching row in r, then result is the same as an inner join. We'd only see a difference where rows are "missing" from r.
0

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%"

1 Comment

The predicate in the WHERE clause effectively negates the "outerness" of the LEFT JOIN. (The conditional test guarantees us that no rows will be returned with a NULL value for tournamentName, so any NULL-valued rows from b created by the "outer join" will be discarded, rendering this equivalent to an inner join.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.