2013-10-26 117 views
0

我需要編寫一個php腳本來處理MySql數據庫中的表。我知道表名,並且我需要通過此子表中的名稱foreign字段知道名稱父表。我怎樣才能用SQL查詢呢?如何在子表中通過名稱foreign字段獲取名稱父表?

UPDATE

SELECT referenced_table_name, referenced_column_name 
FROM information_schema.key_column_usage 
WHERE table_name = '[child_table_name]' 
AND column_name = '[foreign_key_field_name]' 

回答

1
select referenced_table_name 
from information_schema.REFERENTIAL_CONSTRAINTS 
where table_name ='[child_table_name]' 
--and constraint_name ='[foreign_key_constraint_name]' 

UPDATE

select referenced_table_name, 
referenced_column_name 
from information_schema.key_column_usage where table_name ='[child_table_name]' 
and column_name='[foreign_key_constraint_name]'; 
+0

@Andy:檢查更新的版本... – a1ex07

相關問題