我很难把所有事情搞清楚。这个问题来自这个问题,Inserting into a Table joined to another Table by a reference table?。
我想要做的事情的一半是在这个问题中解释的,我试图做的其他一些事情将在这个问题中解释。
我在Teams表中有这些数据(也可以是Projects表)和Members表
+---------+---------------------+----------------------+
| team_id | team_name | team_desc |
+---------+---------------------+----------------------+
| 1 | Toronto Maple Leafs | Team is Awesome! |
| 2 | Vancouver Kanucks | Team Sucks! |
| 3 | Chicago Bulls | Team is Fine! |
| 4 | New York Boys | Nah! It doesn't suck |
+---------+---------------------+----------------------+
和成员表中的这些数据
+-----------+-----------+----------+---------------------------------+---------------+-------------+-------------------------------+------------+------------+-------------+----------+-----------------+
| member_id | firstname | lastname | address | state | country | email | phone | experience | designation | username | password |
+-----------+-----------+----------+---------------------------------+---------------+-------------+-------------------------------+------------+------------+-------------+----------+-----------------+
| 1 | Duncan | Daniel | P.O. Box 470, 2775 Ac Road | NS | Israel | duncan@aol.in | 9288091790 | 1 | Script | duncan | debbyisaweseome |
| 2 | Ivan | Richmond | P.O. Box 797, 906 Cursus Street | New Brunswick | Timor-Leste | ligula.tortor.dictum@Duis.com | 8087179788 | 5 | Storyboard | ivan | ivaniscool |
+-----------+-----------+----------+---------------------------------+---------------+-------------+-------------------------------+------------+------------+-------------+----------+-----------------+
这两个表由这个teams_members表连接,
+------------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+---------+------+-----+---------+----------------+
| teams_members_id | int(11) | NO | PRI | NULL | auto_increment |
| teams_id | int(11) | NO | MUL | NULL | |
| members_id | int(11) | NO | MUL | NULL | |
+------------------+---------+------+-----+---------+----------------+
正如您所见teams_id
& members_id
是来自团队的外键和成员。
现在正如上面提到的问题中的upvoted帖子中所建议的那样,我首先在团队中插入一行,然后在这样的team_members表中插入,
INSERT INTO teams(`team_name`,`team_desc`) values ("New York Boys", "Nah! It doesn't suck");
INSERT INTO teams_members (`members_id`,`teams_id`) VALUES
((SELECT member_id from members WHERE member_id = 2),
(SELECT team_id from teams WHERE team_name = "New York Boys"))
然后尝试选择由此类特定成员创建的团队
SELECT * FROM teams_members, members, teams WHERE members_id = member_id && member_id = 1;
但它没有只显示一个团队,而是给了我这个结果:
+------------------+----------+------------+-----------+-----------+----------+----------------------------+-------+---------+---------------+------------+------------+-------------+----------+-----------------+---------+---------------------+----------------------+
| teams_members_id | teams_id | members_id | member_id | firstname | lastname | address | state | country | email | phone | experience | designation | username | password | team_id | team_name | team_desc |
+------------------+----------+------------+-----------+-----------+----------+----------------------------+-------+---------+---------------+------------+------------+-------------+----------+-----------------+---------+---------------------+----------------------+
| 1 | 3 | 1 | 1 | Duncan | Daniel | P.O. Box 470, 2775 Ac Road | NS | Israel | duncan@aol.in | 9288091790 | 1 | Script | duncan | debbyisaweseome | 1 | Toronto Maple Leafs | Team is Awesome! |
| 1 | 3 | 1 | 1 | Duncan | Daniel | P.O. Box 470, 2775 Ac Road | NS | Israel | duncan@aol.in | 9288091790 | 1 | Script | duncan | debbyisaweseome | 2 | Vancouver Kanucks | Team Sucks! |
| 1 | 3 | 1 | 1 | Duncan | Daniel | P.O. Box 470, 2775 Ac Road | NS | Israel | duncan@aol.in | 9288091790 | 1 | Script | duncan | debbyisaweseome | 3 | Chicago Bulls | Team is Fine! |
| 1 | 3 | 1 | 1 | Duncan | Daniel | P.O. Box 470, 2775 Ac Road | NS | Israel | duncan@aol.in | 9288091790 | 1 | Script | duncan | debbyisaweseome | 4 | New York Boys | Nah! It doesn't suck |
+------------------+----------+------------+-----------+-----------+----------+----------------------------+-------+---------+---------------+------------+------------+-------------+----------+-----------------+---------+---------------------+----------------------+
我的查询中是否缺少某些内容?此外,关于插入部分,还有更好的方法吗?
答案 0 :(得分:1)
SELECT *
FROM teams_members tm, members m, teams t
WHERE tm.members_id = m.member_id
AND m.member_id = 1
AND tm.teams_id = t.team_id
答案 1 :(得分:0)
1)将*替换为您想要获取的列。
2)为表添加别名:... FROM teams_members tm,members m,teams t where ...
3)在其中使用带别名表的字段:m.member_id = tm.member_id
答案 2 :(得分:0)
您需要使用加入来获取团队名称。
SELECT查询需要看起来像这样....
" SELECT team_name FROM teams JOIN teams_members ON teams.teams_id = teams_members.teams_id"
从team表中获取所有行数据使用此查询
" SELECT * FROM团队JOIN teams_members ON teams.teams_id = teams_members.teams_id"