更新查询mysql上的内连接而不是子查询

时间:2017-03-14 20:06:38

标签: mysql subquery inner-join

有人可以告诉我如何使用inner join,而不是我在下面的代码中使用的subquery,我不明白你何时使用一个字符作为名字所以请使用代码的名称。

UPDATE plataforma.cursos 
SET tema = ?, indice = ?, descripcion = ?, fecha = ?, idioma = ?, enlaces = ?, 
nivel = ?, keywords = ?, imagenId = (select imagenId 
                                    from imagenes 
                                    where ubicacion=?), autorId = ?,
escuelasId = ?, categoriaId = ?, subcategoriaId = ? 
WHERE idCurso =  ?;";

它还准备了php mysqli准备好的查询。

编辑: Imagenes表结构

CREATE TABLE IF NOT EXISTS imagenes(
        idImagen INT NOT NULL AUTO_INCREMENT,
        ubicacion VARCHAR(50) NOT NULL,
        PRIMARY KEY(idImagen)
        )
    ENGINE = InnoDB;

Cursos表结构

CREATE TABLE IF NOT EXISTS  cursos (
        idCurso INT NOT NULL AUTO_INCREMENT,
        tema VARCHAR(200) NOT NULL,
        indice VARCHAR(500) NULL,
        descripcion VARCHAR(250)  NULL,
        fecha DATE NOT NULL,
        idioma VARCHAR(12) NOT NULL,
        enlaces VARCHAR(300) NOT NULL,
        nivel VARCHAR(35) NOT NULL,
        keywords TINYTEXT NULL,
        imagenId INT NOT NULL,
        autorId INT NOT NULL,
        escuelasId INT NOT NULL,
        categoriaId INT NOT NULL,
        subcategoriaId INT NULL,
        PRIMARY KEY (idCurso),
        CONSTRAINT refeImagen
            FOREIGN KEY (imagenId)
            REFERENCES imagenes (idImagen)
            ON DELETE RESTRICT
            ON UPDATE CASCADE,
        CONSTRAINT rauthor
            FOREIGN KEY (autorId)
            REFERENCES autores (idAutor)
            ON DELETE RESTRICT
            ON UPDATE CASCADE,
        CONSTRAINT rschool
            FOREIGN KEY (escuelasId)
            REFERENCES escuelas (idEscuela)
            ON DELETE RESTRICT
            ON UPDATE CASCADE,
        CONSTRAINT rctg
            FOREIGN KEY (categoriaId)
            REFERENCES categorias (idCategoria)
            ON DELETE RESTRICT
            ON UPDATE CASCADE,
        CONSTRAINT rsubctg
            FOREIGN KEY (subcategoriaId)
            REFERENCES subcategorias (idSubcategoria)
            ON DELETE RESTRICT
            ON UPDATE CASCADE
        ) 
    ENGINE = InnoDB;

0 个答案:

没有答案