简体   繁体   English

如何根据相关表的结果选择MySQL记录?

[英]How do I select MySQL records based on results from a related table?

I have two tables in this scenario: members and team_members. 在这种情况下,我有两个表:member和team_members。 The members table is pretty self explanatory. 成员表很容易说明。 The team members table stores the member's team information if they are a member of the team. 如果团队成员是团队成员,则团队成员表将存储成员的团队信息。 If there is no row in the team members table that has a member_id of a user, then they are not in a team. 如果团队成员表中没有包含用户的member_id的行,则他们不在团队中。 What I want to do is get all the users that are not members of a team. 我要做的是获取不是团队成员的所有用户。 Should I use left join, inner join, outer join, or just join? 我应该使用左联接,内部联接,外部联接还是仅联接? What would this query look like? 该查询是什么样的?

CREATE TABLE IF NOT EXISTS `members` (
  `member_id` int(15) NOT NULL AUTO_INCREMENT,
  `group_id` int(15) NOT NULL,
  `display_name` text NOT NULL,
  `email_address` text NOT NULL,
  `password` text NOT NULL,
  `status` tinyint(1) NOT NULL,
  `activation_code` varchar(16) NOT NULL,
  `date_joined` text NOT NULL,
  PRIMARY KEY (`member_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `team_members` (
  `team_member_id` int(15) NOT NULL AUTO_INCREMENT,
  `member_id` int(15) NOT NULL,
  `team_id` int(15) NOT NULL,
  `date_joined` text NOT NULL,
  `date_left` text NOT NULL,
  `total_xp` int(15) NOT NULL,
  PRIMARY KEY (`team_member_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

There's several ways to write this query. 有几种编写此查询的方法。

To me this is the easiest to read and understand: 对我来说,这是最容易阅读和理解的:

select * from members where member_id not in (select member_id from team_members).

This is a really simple way to write it. 这是一种非常简单的编写方法。 If you decide you want everything you can quickly comment out the where clause: 如果您决定想要所有内容,则可以快速注释掉where子句:

select m.* from members m left outer join team_members tm on m.member_id = tm.member_id
where tm.member_id is null

This way doesn't seem very popular from the SQL I read but I think it's straightforward: 这种方式在我阅读的SQL中似乎不太流行,但我认为它很简单:

select m.* from members m where not exists
    (select member_id from team_members tm where tm.member_id = m.member_id)

On the face of it, the below query is fine 从表面上看,下面的查询很好

SELECT members.member_id 
FROM   members 
       LEFT OUTER JOIN team_members 
                    ON team_members.member_id = members.member_id 
WHERE  team_members.member_id IS NULL 

This will do, but on reading your question again, you seem to have a date_left column and if you want only those members who have not yet left a team then 可以,但是在再次阅读您的问题时,您似乎有一个date_left列,并且如果只希望那些尚未离开团队的成员,那么

SELECT members.member_id 
FROM   members 
       LEFT OUTER JOIN (SELECT * 
                        FROM   team_members 
                        WHERE  team_members.date_left != '') CURRENT_TEAMS 
                    ON CURRENT_TEAMS.member_id = members.member_id 
WHERE  CURRENT_TEAMS.member_id IS NULL 

SQLFiddle example http://www.sqlfiddle.com/#!2/46b25/6/0 SQLFiddle示例http://www.sqlfiddle.com/#!2/46b25/6/0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM