I have a view table (let's call it view_table
) composed of two tables (let's call them table_1
& table_2
). Let's assume that table_1
has a foreign key on the field fk_field
that references the table fk_table
.
Is there anyway to directly query view_table
in order get the reference table fk_table
for the field fk_field
?
I'm doing this from PHP so I was thinking of doing something along the lines of:
- Parse the
VIEW_DEFINITION
field fromINFORMATION_SCHEMA.VIEWS
in order to get a list of tables that constituteview_table
(e.g.table_1
&table_2
) - For each of those tables, use the this method to get
fk_table
Is there a better (i.e. simpler) approach?
EDIT
The view definition in this case, the field Container
is a foreign key which references a table named Containers
:
CREATE
ALGORITHM = UNDEFINED
DEFINER = `root`@`localhost`
SQL SECURITY DEFINER
VIEW `LIMS_dev`.`DerivativeView` AS
SELECT
`a`.`Derivative` AS `Derivative`,
`a`.`Container` AS `Container`,
`a`.`DerivativeTypeID` AS `DerivativeTypeID`,
`a`.`StorageFormatID` AS `StorageFormatID`,
`a`.`SourceSample` AS `SourceSample`,
`a`.`Parent` AS `Parent`,
`a`.`Date` AS `DerivativeDate`,
`a`.`Active` AS `SampleActive`,
`a`.`User` AS `User`,
`a`.`Notes` AS `Notes`,
`b`.`Shelf` AS `Shelf`,
`b`.`Rack` AS `Rack`,
`b`.`FreezerID` AS `FreezerID`,
`b`.`Active` AS `BoxActive`,
`b`.`Date` AS `ContainerDate`
FROM
(`LIMS_dev`.`Derivatives` `a`
LEFT JOIN `LIMS_dev`.`Containers` `b` ON ((`a`.`Container` = `b`.`Container`)))