Ejemplo utilizando join, union y union all para unir dos tablas

utilizando join, union y union all para unir dos tablas.


aquí el código:

CREATE TABLE pruebas.personas{
id int unsigned not null autoincrement,
nombre varchar(15) not null,
apellido varchar(12) not null,
cedula int unsigned not null,
#COLLLL__ ZEROFILL = QUE RELLENE CON 0 LOS ESPACIOS DISPONIBLES A LA IZQUIERDA EJEMPLO  en un int(3) el valor 3 = 003
edad tinyint unsigned not null,
ciudad varchar(25) not null,
pais varchar(25) not null,
PRIMARY KEY (id);

}ENGINE = InnoDB;
#utilizando join, union y union all para unir dos tablas

#join
select * from ciudades as c
         join paises as p
  on c.codigo_pais = p.codigo.pais
where c.codigo_pais = "CO";

#UNION:

use pruebas;
CREATE TABLE test1(
id int unsigned not null primary key auto_increment,
nombre varchar(12) not null,
edad tinyint not null);

create table test2(
 id int unsigned not null primary key auto_increment,
 nombres varchar(12) not null,
    edad tinyint not null);

insert into test1 values(null,'Nelly',15),(null,'Nahomi',55),(null,'chingon',5);
insert into test2 values(null,'bebe',1),(null,'adol',20),(null,'adult',45);


select * from test1 where edad > 10
union
select * from test2 where edad < 10;

select * from test1 where nombre like "N%"
union
select * from test2 where nombres like "a%";
#union all
select * form test2
union all
select * from test1;

MYSQL

Comentarios

Populares

Buscar en este blog