You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

0 D铆as
6 Hrs
3 Min
41 Seg

Creando nuestra primer consulta anidada

22/34
Resources

Contributions 26

Questions 7

Sort by:

Want to see more contributions, questions and answers from the community?

Hablando de metros, dejo esto por ac谩 馃槑馃殱馃殠馃殞馃殸

Para los que no ven 馃檲 mejor que buscar la imagen, aqu铆 est谩n las l铆neas 馃憖

Ferrer铆a -> L铆nea 6
Pantitlan - L铆nea 1, L铆nea 5, L铆nea 9, L铆nea A
Tacuba -> L铆nea 7, L铆nea 2
Mart铆n Carrera -> L铆nea 4, L铆nea 6

Descubr铆 que se pueden declarar funciones en SQL, as铆 que me puse la tarea de crear una funci贸n con la que le pasara el id de la linea y el nombre de la estaci贸n que queria relacionar y me qued贸 as铆

DELIMITER //
CREATE FUNCTION insertTuple2(id, nameToFilter) RETURN INT
BEGIN

	INSERT INTO `metro_project`.`lines_stations` (line_id, station_id) VALUES
	(id, (SELECT id FROM `metro_project`.`stations` WHERE name=nameToFilter))
	
    RETURN NULL
END //

DELIMITER ;

Lo que me pareci贸 mas interesante fue el DELIMITER (Ya que sirve para cambiar el simbolo que representa el fin de una linea en SQL) y lo determinante que es para que funcione correctamente la sentencia, me pase dos noches sin poder avanzar por no entenderlo. Espero les pique el bicho de la curiosidad.
Saludos

Espero poder sacar provecho de est茅 curso para inicarme como desarrollador para el metro de Bogota D.C.

Mi reto va a ser el Metro de Caracas (excluyendo a las zonas en construcci贸n y el el metro de los Teques)

Para quienes lo esten haciendo del transmilenio Bogot谩

Troncales

<
A-Caracas       |
| D-Cl 80         |
| G-NQS sur       |
| K-Cl 26         |
| B-Norte         |
| E-NQS central   |
| H-Caracas sur   |
| L-kr 10         |
| C-Suba          |
| F-americas      |
| J-Eje ambiental |
| M-Kr 7
> 

Esraciones por troncales
A

<
Tercer milenio        |
| Av jimenez            |
| cll 19                |
| cll 22                |
| cll 26                |
| Profamilia            |
| av 39                 |
| cll 45                |
| Marly                 |
| cll 57                |
| cll 63                |
| Flores                |
| cll 72                |
| cll 76                |
| Heroes  
> 

D

<
 Polo                  |
| Escuela militar       |
| cra 47                |
| cra 53                |
| av 68                 |
| Ferias                |
| Boyac谩                |
| Minuto de Dios        |
| Granja-cra 77         |
| av cali               |
| cra 90                |
| Quirigua              |
| Portal de la 80 
> 

G

<
Comuneros             |
| Santa isabel          |
| SENA                  |
| NQS-cll 30 s          |
| NQS-cll 38-a s        |
| General Santander     |
| Alquer铆a              |
| Venecia               |
| Sevillana             |
| Madelena              |
| Perdomo               |
| Portal del sur        |
| Bosa                  |
| La despensa           |
| Le贸n XIII             |
| Terreros-hospitalC.V  |
| San Mateo 
> 

K

<
Universidades         |
| Centro memoria        |
| Plaza deLaDemocracia  |
| Ciudad universitaria  |
| Corferias             |
| Quinta paredes        |
| Gobernaci贸n           |
| CAN                   |
| Salitre-el greco      |
| El tiempo-maloka      |
| av rojas              |
| Normand铆a             |
| Modelia               |
| Portal el Dorado      |
| Aereopuerto elDorado
> 

B

<
cll 85                |
| Virrey                |
| cll 100               |
| cll 106               |
| Pepe sierra           |
| cll 127               |
| Prado                 |
| Alcal谩                |
| cll 142               |
| cll 146               |
| Mazur茅n               |
| Cardio infantil       |
| Tober铆n               |
| Portal del norte      |
| cll 187               |
| Terminal  > 

E

<
Tigua-san Jose        |
| Guatoque-veraguas     |
| Ricaurte              |
| Paloquemao            |
| CAD                   |
| av el Dorado          |
| U nacional            |
| CampinUAntonioNari帽o  |
| Coliseo               |
| Sim贸n Bol铆var         |
| av Chile              |
| NQS-cll 75            |
| La castellana 
> 

H

<
Hospital              |
| Nari帽o                |
| Fucha                 |
| Restrepo              |
| Olaya                 |
| Quiroga               |
| cll 40 s              |
| Santa Luc铆a           |
| Socorro               |
| Consuelo              |
| Molinos               |
| Portal Usme           |
| Biblioteca            |
| Parque                |
| Portal del Tunal
> 

L

<

 Portal 20 de Julio    |
| Country sur           |
| av 1 de Mayo          |
| Ciudad jardin-UAN     |
| Policarpa             |
| Hospitales            |
| Bicentenario          |
| San victorino         |
| Las nieves            |
| San Diego> 

C

<
San Martin            |
| Rio negro             |
| Suba-cll 95           |
| Suba-cll 100          |
| Puente largo          |
| Shaio                 |
| Humedal c贸rdoba       |
| Niza-cll 127          |
| Suba-av Boyac谩        |
| Gratamira             |
| 21 脕ngeles            |
| Suba-tv 91            |
| La campi帽a            |
| Portal de suba
> 

F

<
De la sabana          |
| San facon-kra 22      |
| CDS-kra 32            |
| Zona industrial       |
| cra 43                |
| Puente aranda         |
| Am茅ricas-kra 53A      |
| Pradera               |
| Marsella              |
| Mundo Aventura        |
| Mandalay              |
| Banderas              |
| Transversal 86        |
| Biblioteca tintal     |
| Patio bonito          |
| Portal deLasAm茅ricas
> 

J

<
Las aguas             |
| Museo del oro
> 

M

<Kra 7 > 

Existe una mejor manera de llenar la tabla lines_stations con solo un Query.
.
Pero para lograr esto, hay que hacer cambios en la tabla de stations. Se tendr铆a que agregar la columna line_id dentro de esta tabla de stations.
.
Yo lo hice de esa forma porque siempre cada estaci贸n va a pertenecer a una l铆nea del metro. Y en caso de las estaciones con m谩s de una l铆nea, para eso nos va a ayudar la tabla pivot.
.
Despues de agregar este campo, hay que agregarle un valor a cada estaci贸n. Ese c贸digo lo tengo ya hecho en este archivo sql: https://github.com/irvingvjuarez/mariadb-course/blob/main/update/update-line-id.sql
.
Una vez que ya todas las estaciones tienen un valor en line_id, entonces ahora si podemos llenar de manera sencilla la tabla lines_stations con el siguiente query

USE cdmx_subway;

INSERT INTO lines_stations (station_id, line_id)
SELECT id, line_id
FROM stations;

Y listo, la tabla pivot se habr谩 llenado de valores de manera r谩pida y sencilla

Tengo suerte porque decid铆 hacer el proyecto con el metro de Buenos Aires que tiene 85 estaciones jeje

Este fue mi codigo para crear la tabla: ``CREATE TABLE `lines_station`(聽 聽 ID BIGINT(20) UNSIGNED AUTO_INCREMENT NOT NULL,聽 聽 LINE_ID BIGINT(20) UNSIGNED NOT NULL,聽 聽 STATION_ID BIGINT(20) UNSIGNED NOT NULL,聽 聽 CREATE_AT TIMESTAMP NOT NULL DEFAULT NOW(),聽 聽 UPDATE_AT TIMESTAMP NOT NULL DEFAULT NOW(), 聽 聽 PRIMARY KEY (ID),聽 聽 CONSTRAINT fk_LINE_ID 聽FOREIGN KEY (LINE_ID)聽 聽 REFERENCES `lines`(id),聽 聽 CONSTRAINT fk_STATION_ID 聽FOREIGN KEY (STATION_ID)聽 聽 REFERENCES `stations`(id))DEFAULT CHARSET = utf8mb4COLLATE = utf8mb4_unicode_ci;``
Este fue mi codigo para crear la tabla: CREATE TABLE `lines\_station`(聽 聽 ID BIGINT(20) UNSIGNED AUTO\_INCREMENT NOT NULL,聽 聽 LINE\_ID BIGINT(20) UNSIGNED NOT NULL,聽 聽 STATION\_ID BIGINT(20) UNSIGNED NOT NULL,聽 聽 CREATE\_AT TIMESTAMP NOT NULL DEFAULT NOW(),聽 聽 UPDATE\_AT TIMESTAMP NOT NULL DEFAULT NOW(), 聽 聽 PRIMARY KEY (ID),聽 聽 CONSTRAINT fk\_LINE\_ID 聽FOREIGN KEY (LINE\_ID)聽 聽 REFERENCES `lines`(id),聽 聽 CONSTRAINT fk\_STATION\_ID 聽FOREIGN KEY (STATION\_ID)聽 聽 REFERENCES `stations`(id))DEFAULT CHARSET = utf8mb4COLLATE = utf8mb4\_unicode\_ci;
```js USE metro_cdmx; INSERT INTO `lines_stations`(line_id, stations_id) VALUES --Insertando primer Ferrer铆a que pertenece a la l铆nea 9 ((SELECT`lines`.`id` FROM `lines` WHERE `lines`.`name`= "l铆nea 9"), (SELECT `stations`. `id` FROM `stations` WHERE `stations`.`name` = "L谩zaro C谩rdenas")), --Insertando L谩zaro C谩rdenas que pertenece a la l铆nea 9 ((SELECT`lines`.`id` From `lines` WHERE `lines`.`name` = "l铆nea 9"), (SELECT `stations`.`id` FROM `stations` WHERE `stations`.`name` = "Ferrer铆a")), --Insertando Pantitl谩n que pertenece a la l铆nea 1, 5, 9, A ((SELECT `lines`.`id` FROM `lines` WHERE `lines`.`name` = "l铆nea 1"), (SELECT `stations`.`id` FROM `stations` WHERE `stations`.`name` = "Pantitl谩n")), ((SELECT `lines`.`id` FROM `lines` WHERE `lines`.`color` = "Amarillo"), (SELECT `stations`.`id` FROM `stations` WHERE `stations`.`name` = "Pantitl谩n")), ((SELECT `lines`.`id` FROM `lines` WHERE `lines`.`color` = "Morado"), (SELECT `stations`.`id` FROM `stations` WHERE `stations`.`name` = "Pantitl谩n")), ((SELECT `lines`.`id` FROM `lines` WHERE `lines`.`color` = "Caf茅"), (SELECT `stations`.`id` FROM `stations` WHERE `stations`.`name` = "Pantitl谩n")), ```
`<` USE metro\_cdmx; INSERT INTO `lines\_stations`(line\_id, stations\_id) VALUES--Insertando primer Ferrer铆a que pertenece a la l铆nea 9((SELECT`lines`.`id` FROM `lines` WHERE `lines`.`name`= "l铆nea 9"), (SELECT `stations`. `id` FROM `stations` WHERE `stations`.`name` = "L谩zaro C谩rdenas")),--Insertando L谩zaro C谩rdenas que pertenece a la l铆nea 9((SELECT`lines`.`id` From `lines` WHERE `lines`.`name` = "l铆nea 9"),(SELECT `stations`.`id` FROM `stations` WHERE `stations`.`name` = "Ferrer铆a")),--Insertando Pantitl谩n que pertenece a la l铆nea 1, 5, 9, A((SELECT `lines`.`id` FROM `lines` WHERE `lines`.`name` = "l铆nea 1"), (SELECT `stations`.`id` FROM `stations` WHERE `stations`.`name` = "Pantitl谩n")),((SELECT `lines`.`id` FROM `lines` WHERE `lines`.`color` = "Amarillo"), (SELECT `stations`.`id` FROM `stations` WHERE `stations`.`name` = "Pantitl谩n")),((SELECT `lines`.`id` FROM `lines` WHERE `lines`.`color` = "Morado"), (SELECT `stations`.`id` FROM `stations` WHERE `stations`.`name` = "Pantitl谩n")),((SELECT `lines`.`id` FROM `lines` WHERE `lines`.`color` = "Caf茅"), (SELECT `stations`.`id` FROM `stations` WHERE `stations`.`name` = "Pantitl谩n")), `>`

Mysql s铆 acepta acentos.

SI COPIASTE TODA LA TABLA DE UNA Y QUERES VER SI LO QUE PUSO EL PROFE ESTA BIEN:

SELECT * FROM metro_cdmx.lines_stations
WHERE line_id = 9 AND station_id = 1;

Y ENCONTRAS EL RESULTADO

This course is amazing!

Lo habia hecho con anterioridad y as铆 tengo creada esa tabla:

CREATE TABLE `lines_stations`(
    `id` BIGINT(20)  UNSIGNED NOT NULL AUTO_INCREMENT,
    `lines_id` BIGINT(20)  UNSIGNED NOT NULL,
    `stations_id` BIGINT(20)  UNSIGNED NOT NULL,
    `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    CONSTRAINT `lines_stations_line_id_foreign` FOREIGN KEY(`lines_id`) REFERENCES `lines`(`id`),
    CONSTRAINT `lines_stations_stations_id_foreign` FOREIGN KEY(`stations_id`) REFERENCES `stations`(`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

![](

La conclucion con respecto al uso de los acentos ser铆a: De preferencia, no lo uses.

USE metro_cdmx;

TRUNCATE TABLE `lines_stations`;

INSERT INTO
    `lines_stations` (lines_id, stations_id)
VALUES
    (
        (
            SELECT
                `lines`.`id`
            FROM
                `lines`
            WHERE
                `lines`.`name` = 'Linea 1'
        ),
        (
            SELECT
                `stations`.`id`
            FROM
                `stations`
            WHERE
                `stations`.`name` = 'Buenavista'
        )
    ),
    (
        (
            SELECT
                `lines`.`id`
            FROM
                `lines`
            WHERE
                `lines`.`name` = 'Linea 1'
        ),
        (
            SELECT
                `stations`.`id`
            FROM
                `stations`
            WHERE
                `stations`.`name` = 'Zaragoza'
        )
    ),
    (
        (
            SELECT
                `lines`.`id`
            FROM
                `lines`
            WHERE
                `lines`.`name` = 'Linea 2'
        ),
        (
            SELECT
                `stations`.`id`
            FROM
                `stations`
            WHERE
                `stations`.`name` = 'G贸mez Far铆as'
        )
    ),
    (
        (
            SELECT
                `lines`.`id`
            FROM
                `lines`
            WHERE
                `lines`.`name` = 'Linea 1'
        ),
        (
            SELECT
                `stations`.`id`
            FROM
                `stations`
            WHERE
                `stations`.`name` = 'Boulevard Puerto A茅reo'
        )
    ),
    (
        (
            SELECT
                `lines`.`id`
            FROM
                `lines`
            WHERE
                `lines`.`name` = 'Linea 3'
        ),
        (
            SELECT
                `stations`.`id`
            FROM
                `stations`
            WHERE
                `stations`.`name` = 'Balbuena'
        )
    );

-- SELECT
--     l.name AS nombre_linea,
--     l.color AS color_lines,
--     ls.lines_id AS id_linea,
--     ls.stations_id AS id_estacion,
--     s.name AS nombre_estacion
-- FROM
--     `lines` AS l
--     INNER JOIN lines_stations AS ls ON l.id = ls.lines_id
--     INNER JOIN stations AS s ON ls.stations_id = s.id;
USE metro_cdmx;

INSERT INTO `lines_stations` (line_id, station_id) VALUES
(
    (
        SELECT `lines`.`id` 
        FROM `lines` 
        WHERE `lines`.`name` = "L铆nea 9"
    ),
    (
        SELECT `stations`.`id` 
        FROM `stations` 
        WHERE `stations`.`name` = "L谩zaro C谩rdenas"
    )
)

Reto

INSERT INTO `lines_stations`(line_id, station_id) VALUES
(
        (
            SELECT `lines`.`id` 
            FROM `lines`
            WHERE `lines`.`name`= "L铆nea 9"
        ),
        (
            SELECT `stations`.`id`
            FROM `stations`
            WHERE `stations`.`name`="L谩zaro C谩rdenas"
        )
),
(
        (
            SELECT `lines`.`id` 
            FROM `lines`
            WHERE `lines`.`name`= "L铆nea 6"
        ),
        (
            SELECT `stations`.`id`
            FROM `stations`
            WHERE `stations`.`name`="Ferrer铆a "
        )
),
(
        (
            SELECT `lines`.`id` 
            FROM `lines`
            WHERE `lines`.`name`= "L铆nea 1"
        ),
        (
            SELECT `stations`.`id`
            FROM `stations`
            WHERE `stations`.`name`="Pantitl谩n"
        )
),
(
        (
            SELECT `lines`.`id` 
            FROM `lines`
            WHERE `lines`.`name`= "L铆nea 5"
        ),
        (
            SELECT `stations`.`id`
            FROM `stations`
            WHERE `stations`.`name`="Pantitl谩n"
        )
),
(
        (
            SELECT `lines`.`id` 
            FROM `lines`
            WHERE `lines`.`name`= "L铆nea 9"
        ),
        (
            SELECT `stations`.`id`
            FROM `stations`
            WHERE `stations`.`name`="Pantitl谩n"
        )
),
(
        (
            SELECT `lines`.`id` 
            FROM `lines`
            WHERE `lines`.`name`= "L铆nea A"
        ),
        (
            SELECT `stations`.`id`
            FROM `stations`
            WHERE `stations`.`name`="Pantitl谩n"
        )
),
(
        (
            SELECT `lines`.`id` 
            FROM `lines`
            WHERE `lines`.`name`= "L铆nea 2"
        ),
        (
            SELECT `stations`.`id`
            FROM `stations`
            WHERE `stations`.`name`="Tacuba"
        )
),
(
        (
            SELECT `lines`.`id` 
            FROM `lines`
            WHERE `lines`.`name`= "L铆nea 7"
        ),
        (
            SELECT `stations`.`id`
            FROM `stations`
            WHERE `stations`.`name`="Tacuba"
        )
),
(
        (
            SELECT `lines`.`id` 
            FROM `lines`
            WHERE `lines`.`name`= "L铆nea 4"
        ),
        (
            SELECT `stations`.`id`
            FROM `stations`
            WHERE `stations`.`name`="Mart铆n Carrera"
        )
),
(
        (
            SELECT `lines`.`id` 
            FROM `lines`
            WHERE `lines`.`name`= "L铆nea 6"
        ),
        (
            SELECT `stations`.`id`
            FROM `stations`
            WHERE `stations`.`name`="Mart铆n Carrera"
        )
);

Es maravilloso todo lo que he aprendido haciendo todo el proceso en cada clase; me salen errores desconocidos que me hacen buscar documentaci贸n, leer parte del manual de mariadb, buscar en foros y a veces despues de buscar por horas, darme cuenta que solo me faltaba una palabra o lo peor era solo un coma.

Son geniales las clases con Retax 鉂わ笍

Mi gran reto es insertar todas las estaciones del metro de medell铆n 馃槂

jajaja, yo esperando esta clase y preguntando 驴Qu茅 pas贸 con esa tabla?