{"id":387,"date":"2011-07-19T00:53:02","date_gmt":"2011-07-19T00:53:02","guid":{"rendered":"http:\/\/tech.avant.net\/q\/?p=387"},"modified":"2012-12-25T22:39:35","modified_gmt":"2012-12-25T22:39:35","slug":"users_not_in_group","status":"publish","type":"post","link":"https:\/\/tech.avant.net\/q\/users_not_in_group\/","title":{"rendered":"sql, users not in group"},"content":{"rendered":"<p>I would like to find all users not in a specific group, given the following database schema:<\/p>\n<pre class=\"sh_sql\">\r\nCREATE TABLE foobar_users (\r\n    user_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,\r\n    username VARCHAR(20) UNIQUE NOT NULL\r\n);\r\n\r\nCREATE TABLE foobar_groups (\r\n    group_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,\r\n    groupname VARCHAR(20) UNIQUE NOT NULL\r\n);\r\n\r\nCREATE TABLE foobar_users_groups (\r\n    user_id INT UNSIGNED NOT NULL,\r\n    group_id INT UNSIGNED NOT NULL,\r\n    PRIMARY KEY(user_id, group_id)\r\n);\r\n<\/pre>\n<p>There are two common approaches to this problem, one is to use an outer-join and the other approach is to use a sub-select. Here is a left outer join example:<\/p>\n<pre class=\"sh_sql\">\r\nSELECT u.user_id, u.username\r\nFROM foobar_users u\r\nLEFT JOIN (foobar_users_groups ug, foobar_groups g)\r\n  ON u.user_id = ug.user_id\r\n  AND g.group_id = ug.group_id\r\n  AND g.groupname = 'DMV'\r\nWHERE\r\n  ug.user_id IS NULL\r\n<\/pre>\n<p>Alternatively, you can use a sub-select, e.g.,<\/p>\n<pre class=\"sh_sql\">\r\nSELECT u.user_id, u.username\r\nFROM foobar_users u\r\nWHERE u.user_id NOT IN (\r\n  SELECT ug.user_id\r\n  FROM foobar_groups g, foobar_users_groups ug\r\n  WHERE ug.group_id = g.group_id\r\n    AND g.groupname = 'DMV'\r\n)\r\n<\/pre>\n<p>My personal preference, and arguably more efficient solution than the above two, is to use a sub-select with <em>NOT EXISTS<\/em>, as follows:<\/p>\n<pre class=\"sh_sql\">\r\nSELECT u.user_id, u.username\r\nFROM foobar_users u\r\nWHERE NOT EXISTS (\r\n  SELECT NULL\r\n  FROM foobar_groups g, foobar_users_groups ug\r\n  WHERE ug.group_id = g.group_id\r\n    AND ug.user_id = u.user_id\r\n    AND g.groupname = 'DMV'\r\n)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I would like to find all users not in a specific group, given the following database schema: CREATE TABLE foobar_users ( user_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, username VARCHAR(20) UNIQUE NOT NULL ); CREATE TABLE foobar_groups ( group_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, groupname VARCHAR(20) UNIQUE NOT NULL ); CREATE TABLE foobar_users_groups ( user_id INT [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[15],"tags":[],"_links":{"self":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/387"}],"collection":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/comments?post=387"}],"version-history":[{"count":6,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/387\/revisions"}],"predecessor-version":[{"id":718,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/387\/revisions\/718"}],"wp:attachment":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/media?parent=387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/categories?post=387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/tags?post=387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}