{"id":2649,"date":"2015-09-21T01:57:44","date_gmt":"2015-09-21T01:57:44","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=2649"},"modified":"2015-09-21T01:57:44","modified_gmt":"2015-09-21T01:57:44","slug":"postgres-join-on-an-array-field","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/postgres-join-on-an-array-field\/","title":{"rendered":"Postgres: JOIN on an array field"},"content":{"rendered":"<p>Say we create two tables (users and groups), where users are in groups:<\/p>\n<pre lang=\"sql\">\ncreate table users (\n  id int unique,\n  name varchar unique\n);\n\ncreate table groups (\n  id int unique,\n  name varchar unique,\n  member_users int[],\n  member_groups int[]\n);\n<\/pre>\n<p>The groups own the user memberships, rather than having an intermediate table with the relation.<\/p>\n<p>Now, we want to join them, so we can simply do this:<\/p>\n<pre lang=\"sql\">\nselect *\nfrom groups join users\n  on users.id = ANY (groups.member_users)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Say we create two tables (users and groups), where users are in groups: create table users ( id int unique, name varchar unique ); create table groups ( id int unique, name varchar unique, member_users int[], member_groups int[] ); The groups own the user memberships, rather than having an intermediate table with the relation. Now, &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/postgres-join-on-an-array-field\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Postgres: JOIN on an array field&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[160,437,523],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2649"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=2649"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2649\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=2649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=2649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=2649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}