2016-04-13 246 views
0

所有列名稱我有選擇的所有列名的MySql的select查詢:選擇從MySQL數據庫

SELECT info.COLUMN_NAME AS `NAME`, info.TABLE_SCHEMA AS `TABLESCHEMA`, 
    info.TABLE_NAME AS `TABLENAME`, info.ORDINAL_POSITION AS `POSITION`, 
    info.IS_NULLABLE AS `ISNULLABLE`, info.DATA_TYPE AS `DATATYPE`, 
    tc.CONSTRAINT_TYPE AS `CONSTRAINTTYPE`, 
    kcufk.TABLE_SCHEMA AS `REFRENCESCHEMA`, 
    kcufk.TABLE_NAME AS `REFRENCETABLE`, 
    kcufk.COLUMN_NAME AS `REFRENCECOLUMN` FROM 
    INFORMATION_SCHEMA.`COLUMNS` info LEFT OUTER JOIN INFORMATION_SCHEMA.`KEY_COLUMN_USAGE` kcu 
    ON kcu.COLUMN_NAME = info.COLUMN_NAME AND kcu.TABLE_NAME = info.TABLE_NAME LEFT OUTER JOIN 
    INFORMATION_SCHEMA.`TABLE_CONSTRAINTS` tc ON tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME 
    AND tc.TABLE_NAME = kcu.TABLE_NAME LEFT OUTER JOIN INFORMATION_SCHEMA.`REFERENTIAL_CONSTRAINTS` 
    rk ON rk.CONSTRAINT_NAME = tc.CONSTRAINT_NAME LEFT OUTER JOIN INFORMATION_SCHEMA.`KEY_COLUMN_USAGE` 
    kcufk ON kcufk.CONSTRAINT_NAME = rk.UNIQUE_CONSTRAINT_NAME WHERE info.TABLE_NAME = 'reportsetting' AND 
    info.TABLE_SCHEMA = 'core' ORDER BY info.ORDINAL_POSITION ASC 

的問題是在執行這個查詢,外鍵來了多次。

What am I doing wrong in this query?

回答

1

更新,其中加入的條件查詢,這應該給你找什麼

 
    SELECT info.COLUMN_NAME AS `NAME`, info.TABLE_SCHEMA AS `TABLESCHEMA`, 
    info.TABLE_NAME AS `TABLENAME`, info.ORDINAL_POSITION AS `POSITION`, 
    info.IS_NULLABLE AS `ISNULLABLE`, info.DATA_TYPE AS `DATATYPE`, 
    tc.CONSTRAINT_TYPE AS `CONSTRAINTTYPE`, 
    kcufk.TABLE_SCHEMA AS `REFRENCESCHEMA`, 
    kcufk.TABLE_NAME AS `REFRENCETABLE`, 
    kcufk.COLUMN_NAME AS `REFRENCECOLUMN` 

    FROM INFORMATION_SCHEMA.`COLUMNS` info 
    JOIN INFORMATION_SCHEMA.`KEY_COLUMN_USAGE` kcu 
     ON kcu.COLUMN_NAME = info.COLUMN_NAME 
     AND kcu.TABLE_NAME = info.TABLE_NAME 

    JOIN INFORMATION_SCHEMA.`TABLE_CONSTRAINTS` tc 
     ON tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME 
     AND tc.TABLE_NAME = kcu.TABLE_NAME 

    JOIN INFORMATION_SCHEMA.`REFERENTIAL_CONSTRAINTS` rk 
     ON rk.CONSTRAINT_NAME = tc.CONSTRAINT_NAME 
     AND rk.TABLE_NAME = kcu.TABLE_NAME 

    JOIN INFORMATION_SCHEMA.`KEY_COLUMN_USAGE` kcufk 
     ON kcufk.CONSTRAINT_NAME = rk.UNIQUE_CONSTRAINT_NAME 
     AND kcufk.TABLE_NAME = rk.TABLE_NAME 

    WHERE info.TABLE_NAME = 'reportsetting' 
     AND info.TABLE_SCHEMA = 'core' 
    ORDER BY info.ORDINAL_POSITION ASC 
+0

: - 在執行這個查詢O/P來一次,但其REFRENCETABLE沒有顯示正確。它顯示相同的表名,即報告設置 – Sanjiv

+0

: - 即使'REFRENCECOLUMN'也不能正確顯示。它顯示相同的表引用列名 – Sanjiv

+0

我得到了解決方案..其實我選擇table_name而不是reference_table_name。 – Sanjiv