Fetching Data from Multiple Tables using Joins
7 Apr
Normalization is almost universally applied to relational databases such as MySQL in order to optimize tables for general-purpose querying and to rid them of certain undesirable characteristics that could lead to a loss of data integrity. Doing so tends to promote better accuracy of queries, but it also leads to queries that take a little more work to develop, as the data may be spread amongst several tables. In today’s article, we’ll learn how to fetch data from multiple tables by using joins.
Two Syntaxes
There are two accepted syntax styles for writing joins: ANSI-style joins and theta-style joins. ANSI syntax uses the JOIN and ON keywords, as in the following example:
field2,
FROM my_table t1
JOIN my_other_table t2
ON t1.primary_id_field = t2.foreign_key_id_field
WHERE t1.lastname = ‘Smith’;
The JOIN keyword is used to separate the names of the tables being joined, and the ON clause contains the relation showing which column is being used as the join key.
(more…)

