Facebook Fanpage


  • Enviar formulario con ajax jquery

    En el siguiente ejemplo, os mostraré como enviar un formulario vía AJAX, este proceso facilitará las consultas necesarias a PHP, todo ello...
  • Descifrando la Seguridad: El Fascinante Mundo de los Criptosistemas de Clave Pública

    Entrada 1: La Revolución de la Criptografía Asimétrica¿Alguna vez te has preguntado cómo puedes enviar información confidencial a través de internet...
  • Cómo configurar un servidor FTP sobre CentOS 7

    Instalación y puesta en marcha de vsftpd Instalar vsftpd en CentOS 7 es sencillo, en esta versión todavía se sigue usando Yum, el gestor de...
  • Oracle Dumpdir – Import, Export y el uso de Directorios.

    Oracle Dumpdir  Import, Export y el uso de Directorios. Cuando tenemos diferentes objetos de la base, ya sea una tabla, datos, etc y...
  • Solución a ORA-65096: invalid common user or role name en Oracle

    Solución a ORA-65096: invalid common user or role name en Oracle Hola a todos, hoy explicaré como solucionar un problema común en Oracle al...
  • Asignar permisos correctos a carpetas 755 y ficheros 644 de forma masiva

    Asignar permisos correctos a carpetas 755 y ficheros 644 de forma masiva Por defecto, en el FTP de una web los permisos deben ser los siguientes:...
  • Publicar aplicación WAR/JSP/SERVLET/TOMCAT/MYSQL en servidor dedicado/vps cPanel

    Publicar aplicación war con conexión a mysql.1. Ingresar al cPanel2. En la opción "Mysql Bases de datos", crear una base de datos, un usuario de...
  • Generar jar con NetBeans incluyendo Bibliotecas Externas Java

    Hola amigos, después de buscar un poco acerca de como incluir las librerías que utilizamos en nuestros proyectos Java...
  • Subir imagen al servidor con Yii Framework y eliminar imagenes del servidor

    En esta ocacion les traigo la manera de como subir imagenes al servidor y una ves subidas las imagenes como eliminarlas del servidor, este ejemplo es...
  • crear host virtual

    Cómo crear un VirtualHost en ubuntu y apache En este post veremos qué es y cómo crear un virtual host utilizando ubuntu y apache. ¿Qué es un...
  • Enviar formulario con ajax jquery

    En el siguiente ejemplo, os mostraré como enviar un formulario vía AJAX, este proceso facilitará las consultas necesarias a PHP, todo ello...
  • Descifrando la Seguridad: El Fascinante Mundo de los Criptosistemas de Clave Pública

    Entrada 1: La Revolución de la Criptografía Asimétrica¿Alguna vez te has preguntado cómo puedes enviar información confidencial a través de internet...
Previous Next

viernes, 25 de enero de 2013

MySQL LEFT, RIGHT JOIN tutorial


MySQL LEFT, RIGHT JOIN tutorial

MySQL joins are hard for beginners. At least for me when I was beginner.
I will try to explain the joins in the simplest possible way.
Join in MySQL is a query where you can join one or more tables.
For example we have two tables: products and buyers with the following structures.
Table products:
mysql> SELECT * FROM products;
+----+--------------+--------------+
| id | product_name | manufacturer |
+----+--------------+--------------+
|  1 | Shoes        | Company1     |
|  2 | Laptop       | Company2     |
|  3 | Monitor      | Company3     |
|  4 | DVD          | Company4     |
+----+--------------+--------------+
4 rows in set (0.00 sec)
Table buyers:
mysql> SELECT * FROM buyers;
+----+------+------------+----------+
| id | pid  | buyer_name | quantity |
+----+------+------------+----------+
|  1 |    1 | Steve      |        2 |
|  2 |    2 | John       |        1 |
|  3 |    3 | Larry      |        1 |
|  4 |    3 | Michael    |        5 |
|  5 | NULL | Steven     |     NULL |
+----+------+------------+----------+
5 rows in set (0.00 sec)

Left Join

mysql> SELECT buyer_name, quantity, product_name FROM buyers LEFT JOIN products ON
 buyers.pid=products.id;
+------------+----------+--------------+
| buyer_name | quantity | product_name |
+------------+----------+--------------+
| Steve      |        2 | Shoes        |
| John       |        1 | Laptop       |
| Larry      |        1 | Monitor      |
| Michael    |        5 | Monitor      |
| Steven     |     NULL | NULL         |
+------------+----------+--------------+
5 rows in set (0.00 sec)
What happened?
Mysql starts with the left table (buyers). For each row from the table buyers mysql scans the table products, finds the id of the product and returns the product name. Then the product name is joined with the matching row from the table buyers. For unmatched rows it returns null.
To make it simpler, the above query is same as (except the unmatched rows are not returned):
mysql> SELECT buyers.buyer_name, buyers.quantity, products.product_name FROM buyer
s,products WHERE buyers.pid=products.id;
+------------+----------+--------------+
| buyer_name | quantity | product_name |
+------------+----------+--------------+
| Steve      |        2 | Shoes        |
| John       |        1 | Laptop       |
| Larry      |        1 | Monitor      |
| Michael    |        5 | Monitor      |
+------------+----------+--------------+
4 rows in set (0.00 sec)

Right Join

mysql> SELECT buyer_name, quantity, product_name FROM buyers RIGHT JOIN products ON 
buyers.pid=products.id;
+------------+----------+--------------+
| buyer_name | quantity | product_name |
+------------+----------+--------------+
| Steve      |        2 | Shoes        |
| John       |        1 | Laptop       |
| Larry      |        1 | Monitor      |
| Michael    |        5 | Monitor      |
| NULL       |     NULL | DVD          |
+------------+----------+--------------+
5 rows in set (0.00 sec)
What happens here is Mysql starts with the Right table (products). For each id from the table products MySQL scans the left table - buyers to find the matching pid. When it finds the matching pid it returns the buyer_name and the quantity. For unmatched rows it returns null. From my example above it returns NULL for DVD because no one bought DVD.

pagina oficial: http://phpweby.com/tutorials/mysql/32

0 comentarios:

Publicar un comentario