0

El siguiente código funciona bien para lo que necesito, tiene dos consultas anidadas, creo que sería bueno que fuera una sola, pero de ella requiero 2 resultados no uno, a saber: REF_COL_NAME y REF_NAME.

Pregunta: ¿Hay alguna forma de modificar la consulta para que devuelva los mismos resultados, sin usar consultas anidadas?

SELECT
conf.TABLE_SCHEMA,
conf.TABLE_NAME,
conf.ORDINAL_POSITION,
conf.COLUMN_NAME,
conf.DATA_TYPE,
REPLACE(REPLACE(REPLACE(conf.COLUMN_TYPE, conf.DATA_TYPE, ''), '(', ''), ')', '') AS 'DATA_LENGTH',
conf.COLUMN_KEY,
(
    SELECT inncol.REF_COL_NAME
    FROM INNODB_SYS_FOREIGN_COLS AS inncol, 
    INNODB_SYS_FOREIGN as inn
    WHERE inncol.ID = inn.ID
    AND inn.FOR_NAME = CONCAT(CONCAT(conf.TABLE_SCHEMA,'/'),conf.TABLE_NAME)
    AND conf.COLUMN_KEY = 'MUL'
) as 'REF_COL_NAME',
(
    SELECT REPLACE(inn2.REF_NAME, CONCAT(conf.TABLE_SCHEMA,'/'), '')
    FROM INNODB_SYS_FOREIGN_COLS AS inncol2, 
    INNODB_SYS_FOREIGN as inn2
    WHERE inncol2.ID = inn2.ID
    AND inn2.FOR_NAME = CONCAT(CONCAT(conf.TABLE_SCHEMA,'/'),conf.TABLE_NAME)
    AND conf.COLUMN_KEY = 'MUL'
) as 'REF_NAME'
FROM
information_schema.COLUMNS as conf,
INNODB_SYS_FOREIGN_COLS AS inncol,
INNODB_SYS_FOREIGN as inn
WHERE conf.TABLE_SCHEMA != 'information_schema'
AND conf.TABLE_SCHEMA != 'mysql'
AND conf.TABLE_SCHEMA != 'performance_schema'
AND conf.TABLE_SCHEMA != 'phpmyadmin'
AND inncol.ID = inn.ID
ORDER by conf.TABLE_SCHEMA
AND conf.TABLE_NAME
AND conf.ORDINAL_POSITION;
1
  • 1
    Con consultas anidadas, como les llamas, no se puede, pero si podrías hacerlo con un left join.
    – jachguate
    Commented el 8 may. a las 15:00

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.