Mysql select from multiple tables

You can select data from multiple tables with one query. Here is two database table.

Table 1. Documents

number
Title
Text
cat
1
About me
example document 1....
1
2
my home
example document 2...
1
3
Catching fishes
example document 3...
3

Table 2. Categories

id
Name
1
Personal
2
Dogs
3
fishes

When you look at the table.1 the row named cat is Category number. The image below explains how to use two table at same time. The important part is WHERE clause.

 

multiple-mysql-table

 

Query:

SELECT * FROM Documents,Categories WHERE Documents.cat = Categories.id

Result table witn this select query:

number
Title
Text
cat
id
name
1
About me
example document 1....
1
1
Personal
2
my home
example document 2...
1
1
Dogs
3
Catching fishes
example document 3...
3
3
fishes

Bold rows came from second table.

You can specify selected rows.

 

 

SELECT Documents.title,Categories.name FROM Documents,Categories WHERE Documents.cat = Categories.id

here is result:

Title
name
About me
Personal
my home
Dogs
Catching fishes
fishes

Related: Mysql double tableSelect double table mysqlMysq Select

Comments about : Mysql tutorial