我正在使用LAMP并通过phpMyAdmin维护MySQl。每次我想在InnoDB表上构建一个多表查询时,我都厌倦了键入JOIN。
有没有人知道从外键生成JOIN语句的某些功能,插件,方法或资源?
答案 0 :(得分:0)
我最终编写了一个从所有外键构建JOIN
语句的查询:
CREATE VIEW Joins_All AS
SELECT
concat(" JOIN ",TABLE_NAME," ON ",
TABLE_NAME,".",COLUMN_NAME," = ",
REFERENCED_TABLE_NAME,".", REFERENCED_COLUMN_NAME)
AS myJoin
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE REFERENCED_TABLE_NAME IS NOT NULL
UNION
SELECT
concat(" JOIN ",REFERENCED_TABLE_NAME," ON ",
TABLE_NAME,".",COLUMN_NAME," = ",
REFERENCED_TABLE_NAME,".", REFERENCED_COLUMN_NAME)
AS myJoin
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE REFERENCED_TABLE_NAME IS NOT NULL
ORDER BY myJoin ASC
它生成这样的东西:
JOIN区域ON Area_Tare.Id_Area = Area.Id_Area
JOIN Area_Tare ON Area_Tare.Id_Area = Area.Id_Area
加入Insect ON Insect.Id_Insect_Class = Insect_Class.Id_Insect_Class
加入Insect on Insect_Insumo_Formula_Ingredient.Id_Insect = Insect.Id_Insect
我保存了这个文件并复制/粘贴了相应的行,我需要它们或者我使用所需表格的名称过滤视图
这可以很容易地在php中进行调整(我现在不会这样做)以创建基于外键的GUI查询构建器。任何决定这样做的人,请随意使用/改编此代码。