You join two tables in MariaDB using the SQL JOIN statement. Here is a basic JOIN example between two tables:
Let’s consider we have two tables:
1. `employees` table:
id | name | title |
—— | ———- | ———— |
1 | Bob | Engineer |
2 | Alice | Designer |
3 | Tom | Manager |
1. `dept` table:
id | department |
—— | —————— |
1 | IT |
2 | PR |
3 | HR |
If you want to join these two tables by ID, you can use the following SQL statement:
```
SELECT employees.name, employees.title, dept.department
FROM employees
INNER JOIN dept ON employees.id = dept.id;
```
This will give you a result like this:
name | title | department |
———- | ————— | —————— |
Bob | Engineer | IT |
Alice | Designer | PR |
Tom | Manager | HR |
In this example, `INNER JOIN` returns only the records where there is a match in both the `employees` and `dept` table.
There are different types of SQL JOINs:
- (INNER) JOIN: Returns records that have matching values in both tables
- LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right table
- RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the left table
- FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table
Please, replace the `INNER JOIN` keyword with `LEFT JOIN`, `RIGHT JOIN` or `FULL JOIN` if you need other types of joins.