sábado, 26 de octubre de 2024

INNER JOINS

 mysql> select * from sales;

+----+--------------+-------------+

| id | product_name | sale_amount |

+----+--------------+-------------+

|  1 | Product A    |      100.50 |

|  2 | Product B    |       75.25 |

|  3 | Product A    |      120.75 |

|  4 | Product C    |       50.00 |

|  5 | Product B    |       90.80 |

|  6 | Product R    |      300.00 |

+----+--------------+-------------+

6 rows in set (0.00 sec)


mysql> select * from clients;

+-----------+-------------+-------------------+

| client_id | client_name | registration_date |

+-----------+-------------+-------------------+

|         1 | Alice       | 2023-01-10        |

|         2 | Bob         | 2023-02-15        |

|         3 | Charlie     | 2023-03-20        |

|         4 | Ambiorix    | 2024-10-06        |

|         5 | Many        | 2024-10-11        |

+-----------+-------------+-------------------+

5 rows in set (0.00 sec)


mysql> select c.client_name,c.client_id,s.id,s.product_name from clients as c INNER JOIN sales as s on s.id=c.client_id;

+-------------+-----------+----+--------------+

| client_name | client_id | id | product_name |

+-------------+-----------+----+--------------+

| Alice       |         1 |  1 | Product A    |

| Bob         |         2 |  2 | Product B    |

| Charlie     |         3 |  3 | Product A    |

| Ambiorix    |         4 |  4 | Product C    |

| Many        |         5 |  5 | Product B    |

+-------------+-----------+----+--------------+

5 rows in set (0.00 sec)


mysql> select c.client_name,c.client_id,s.id,s.product_name from clients as c LEFT JOIN sales as s on s.id=c.client_id;

+-------------+-----------+------+--------------+

| client_name | client_id | id   | product_name |

+-------------+-----------+------+--------------+

| Alice       |         1 |    1 | Product A    |

| Bob         |         2 |    2 | Product B    |

| Charlie     |         3 |    3 | Product A    |

| Ambiorix    |         4 |    4 | Product C    |

| Many        |         5 |    5 | Product B    |

+-------------+-----------+------+--------------+

5 rows in set (0.00 sec)


mysql> select c.client_name,c.client_id,s.id,s.product_name from clients as c RIGHT JOIN sales as s on s.id=c.client_id;

+-------------+-----------+----+--------------+

| client_name | client_id | id | product_name |

+-------------+-----------+----+--------------+

| Alice       |         1 |  1 | Product A    |

| Bob         |         2 |  2 | Product B    |

| Charlie     |         3 |  3 | Product A    |

| Ambiorix    |         4 |  4 | Product C    |

| Many        |         5 |  5 | Product B    |

| NULL        |      NULL |  6 | Product R    |

+-------------+-----------+----+--------------+


No hay comentarios:

Publicar un comentario