Question
#1

So recently my friend gave me his script, observing it I found this query.
PHP код:
mysql_tquery(mysql"SELECT h.*, p.name as name FROM `houses_data` h LEFT JOIN players_data p ON h.owner = p.id""AppendHousesInfo"); 
Well I never used a query like that so I have no idea what does it do? Anyone can explain me?
Reply
#2

A join fetches data from multiple tables at once based on one or more common columns (the "ON" part of the query). So if, for example, the "owner" column in the houses table says 1042 then that means that the player with id 1042 is the owner of that house. The engine then looks through the players table to find the player with id 1042 and then "joins" that to the result.

There are different types of joins of which the most commonly used one are inner joins and left joins. An inner join intersects two tables and only returns those rows where both of the matched columns are not null. In this example it would mean that houses that don't have an owner would not be included in the result.
A left join returns all rows from the left table (houses in this case) and matches it with corresponding data from the right table (players in this case). If there is no related data then those columns are null. In this case it would mean that a house without an owner would have the player's name returned as null.

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)