Saltar al contenido

unir dos tablas ejemplo de código mysql

Nuestros programadores estrellas agotaron sus provisiones de café, por su búsqueda a tiempo completo por la resolución, hasta que Lucián encontró la contestación en Bitbucket y hoy la compartimos contigo.

Ejemplo 1: cómo unir tablas en sql

JOINING 2TablesinsqlSELECT X.Column_Name , Y.Column_Name2
FROM TABLES1_NAME X 
INNERJOIN TABLES2_NAME Y ON X.Primary_key = Y.Foreign_key;--FOR EXAMPLE--GET THE FIRST_NAME AND JOB_TITLE--USE EMPLOYEES AND JOBS TABLE--THE RELATIONSHIP IS JOB_IDSELECT E.FIRST_NAME , J.JOB_TITLE
FROM EMPLOYEES E
INNERJOIN JOBS J ON J.JOB_ID = E.JOB_ID;

Ejemplo 2: mysql une dos tablas

SELECT user_id, user_name
FROM users
UNIONSELECT organization_id, organization_name
FROM organizations

Ejemplo 3: cómo combinar 2 tablas en MySQL

createtable yourTableName
(select*from yourTableName1
)UNION(select*from yourTableName2
);

Ejemplo 4: combinación de MySQL

A relational database consists of multiple related tables linking together using common columns which are known asforeignkeycolumns. Because of this,datain each tableis incomplete from the business perspective.
MySQL supports the followingtypesof joins:

InnerjoinLeftjoinRightjoinCrossjoin

The following shows the basic syntax of the innerjoin clause that joins two tables table_1 and table_2:

SELECT column_list
FROM table_1
INNERJOIN table_2 ON join_condition;SELECT column_list
FROM table_1
INNERJOIN table_2 USING(column_name);SELECT column_list 
FROM table_1 
LEFTJOIN table_2 USING(column_name);
Here is the syntax of the rightjoin:

SELECT column_list 
FROM table_1 
RIGHTJOIN table_2 ON join_condition;

The following shows the basic syntax of the crossjoin clause:

SELECT select_list
FROM table_1
CROSSJOIN table_2;

Ejemplo 5: tipos de combinaciones en mysql

Joins are used withselect statement. it is used toselectdatafrom multiple table.Types:
MySQL INNERJOIN(orsimplejoin)
MySQL LEFTOUTERJOIN(orLEFTJOIN)
MySQL RIGHTOUTERJOIN(orRIGHTJOIN)InnerJOIN :
The INNERJOINis used toreturnallrowsfrom multiple tableswhere the join condition is satisfied. It is the most common typeofjoin.LeftOuterJoin:
The LEFTOUTERJOINreturnsallrowsfrom the left hand table specified in the ON condition and only those rowsfrom the other tablewhere the join condition is fulfilled.RightOuterJoin:
The RightOuterJoinreturnsallrowsfrom the RIGHT-hand table specified in the ON condition and only those rowsfrom the other tablewhere he join condition is fulfilled.

valoraciones y comentarios

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *