The FROM clause in MySQL is used to specify the table or tables from which you want to retrieve data in a query. It defines the source table or tables for the query. Here's an example of using the FROM clause:
Consider two tables: customers and orders, with the following structures:
Syntax:
CREATE TABLE customers (
id INT PRIMARY KEY,
name VARCHAR(50),
email VARCHAR(50)
);
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
product VARCHAR(50),
quantity INT
);
Suppose we want to retrieve all orders along with the customer information. We can use the FROM clause to specify both tables and join them based on the common customer_id column:
Syntax:
SELECT * FROM orders JOIN customers ON orders.customer_id = customers.id;
In the above example, the FROM clause specifies two tables: orders and customers. The JOIN keyword is used to combine the rows from both tables based on the customer_id column. By using the ON keyword, we define the join condition.
The result of the query will include all columns from both tables where the customer_id matches between the orders and customers tables.
You can also use other clauses like WHERE, GROUP BY, HAVING, and ORDER BY in combination with the FROM clause to further refine your query.
Remember to replace orders and customers with the actual names of your tables, and adjust the join condition based on your table structure and requirements.