FROM - MySQL Clauses
In MySQL, the FROM
clause is used to specify the tables from which you want to select data. This clause is required for any SELECT statement that retrieves data from a specific table or tables. In this tutorial, we'll discuss how to use the FROM
clause in MySQL.
Syntax
The basic syntax for the FROM
clause in MySQL is as follows:
SELECT column1, column2, ...
FROM table1, table2, ...
WHERE condition;
The FROM
clause is used to list the tables from which you want to retrieve data. Multiple tables can be listed in the FROM
clause, separated by a comma. You can then select specific columns from these tables using the SELECT
statement, and filter the results using the WHERE
clause.
Example
Let's say we have a database with two tables: customers
and orders
. We want to retrieve a list of all customers and their orders. Here's how we can use the FROM
clause in MySQL:
SELECT customers.*, orders.*
FROM customers, orders
WHERE customers.id = orders.customer_id;
In this example, we used the FROM
clause to list both the customers
and orders
tables. We then used the SELECT
statement to retrieve all columns from both tables using the .*
wildcard. Finally, we joined these tables using the WHERE
clause to get only those rows where the id
from the customers
table matched the customer_id
in the orders
table.
Output
When we run the example code above, we'll get a list of all customers and their orders, as follows:
+----+----------+------+------------+--------+---------------------+---------------------+---------------------+
| id | name | age | email | id | order_date | shipped_date | customer_id |
+----+----------+------+------------+--------+---------------------+---------------------+---------------------+
| 1 | John Doe | 35 | john@doe.com | 100 | 2021-01-01 00:00:00 | 2021-01-02 00:00:00 | 1 |
| 1 | John Doe | 35 | john@doe.com | 101 | 2021-01-03 00:00:00 | 2021-01-03 00:00:00 | 1 |
| 2 | Jane Doe | 30 | jane@doe.com | 102 | 2021-01-02 00:00:00 | 2021-01-04 00:00:00 | 2 |
+----+----------+------+------------+--------+---------------------+---------------------+---------------------+
This output shows a list of all customers and their corresponding orders.
Explanation
In the example above, we used the FROM
clause with two tables: customers
and orders
. We then used the SELECT
statement to retrieve all columns from both tables. Finally, we joined these tables using the WHERE
clause to match the id
from the customers
table with the customer_id
in the orders
table.
Use
The FROM
clause is required for any SELECT statement that retrieves data from a specific table or tables. It allows you to specify the tables from which you want to select data, and can also be used to join multiple tables together.
Important Points
- You can list multiple tables in the
FROM
clause, separated by a comma. - You can join multiple tables together using the
WHERE
clause, as shown in the example above. - The
FROM
clause is required for any SELECT statement that retrieves data from a specific table or tables.
Summary
In this tutorial, we discussed how to use the FROM
clause in MySQL. We covered the syntax, example, output, explanation, use, and important points of the FROM
clause. With this knowledge, you can now use the FROM
clause in your MySQL queries to select data from one or more tables.