如何使用 5 个不同的表 mysql 创建内部查询 [英] How to create the inner query wtih 5 different tables mysql
问题描述
Having 5 tables
Table a_dates = id,
Table b_types = id, a_date_id, c_type_id,
Table c_types = id, name,
Table d_profiles = id, name, profile_type
Table e_ps = id, a_date_id, d_profile_id
从 a_dates 需要得到 b_types,...然后从 b_types 需要 c_types 名称,... 然后将 c_types 名称与 d_profiles 名称进行比较并得到 d_profiles id.... 如果相等则在 e_ps 中创建一个带有 a_date_id、d_profile_id 的记录.
From a_dates Need to get b_types,...then from b_types needs c_types name,... Then compare c_types name with d_profiles name and get d_profiles id.... if equals then create a records in e_ps with a_date_id, d_profile_id.
任何人都可以帮助我从内部联接中获取查询.
Could any one please help me in getting the query from inner join.
我试过了,它是不完整的查询
I tried like, it is incomplete query
INSERT INTO e_ps(id,a_date_id,a_date_type,d_profile_id,c_id)
SELECT '',a.id,'A',dp.id,'67' FROM d_profiles dp
INNER JOIN a_dates a ON {HERE I NEED NAME MATCHING WITH c_types name} = dp.name and dp.profile_type = 'A'
INNER JOIN a_dates ON a.id = a_dates.id
LEFT OUTER JOIN e_ps eps ON eps.a_date_type = 'A' AND eps.a_date_id = a_dates.id
WHERE eps.a_date_id IS NULL
推荐答案
这似乎是一个比较简单的 JOIN:-
This seems to be a relatively simple JOIN:-
INSERT INTO e_ps(id, a_date_id, d_profile_id)
SELECT NULL, a_dates.id, d_profiles.id
FROM a_dates
INNER JOIN b_types ON a_dates.id = b_types.a_date_id
INNER JOIN c_types ON b.c_type_id = c.id
INNER JOIN d_profiles ON c_types.name = d_profiles.name
连接有多种类型,我怀疑您会感到困惑.简而言之:-
With joins there are several types, and I suspect you are getting confused. Briefly:-
- 使用 INNER JOIN 查找 BOTH 表上的匹配项.如果不匹配没有记录返回.
- 使用 LEFT OUTER JOIN 从左边的表中获取一条记录并在右边的桌子上寻找匹配项.如果比赛很棒,但如果不是那么它仍然会带回一行,但来自右边的表只有 NULL 值.
- RIGHT OUTER JOIN 非常相似,只是表格反转(包括我在内的大多数人都避免使用它,因为它没有大多数时候都有优势,但只会让事情变得混乱).
- 使用 FULL OUTER JOIN 从双方获取记录,无论是他们匹配与否.如果它们匹配,则两者的列都是返回,如果不匹配,则返回其中的列.不是MySQL 不支持 FULL OUTER JOIN(虽然有模仿它的方法).
- CROSS JOIN 连接 2 个表的每个组合.这些用于当没有可匹配的公共列,但您需要所有组合.例如,如果您想要一张包含所有员工和所有天数的表格您将交叉的每个员工的一周加入一张表针对员工表的一周(然后对于有用的数据,您可能LEFT OUTER JOIN 一个假期表到结果中).
这篇关于如何使用 5 个不同的表 mysql 创建内部查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!