<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vitaly Platonov</title>
    <description>The latest articles on DEV Community by Vitaly Platonov (@vitalyplatonov).</description>
    <link>https://dev.to/vitalyplatonov</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F136215%2F2b2de8af-bdcc-4ba7-89ac-f4f187fb76f5.jpeg</url>
      <title>DEV Community: Vitaly Platonov</title>
      <link>https://dev.to/vitalyplatonov</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vitalyplatonov"/>
    <language>en</language>
    <item>
      <title>Postgres. Search Array type columns</title>
      <dc:creator>Vitaly Platonov</dc:creator>
      <pubDate>Fri, 06 Sep 2019 13:56:37 +0000</pubDate>
      <link>https://dev.to/vitalyplatonov/postgres-search-array-type-columns-44ea</link>
      <guid>https://dev.to/vitalyplatonov/postgres-search-array-type-columns-44ea</guid>
      <description>&lt;p&gt;Say we have a table with a column of an &lt;a href="https://www.postgresql.org/docs/11/arrays.html"&gt;array type&lt;/a&gt;. At some point, we want to be able to select records with a specific value(s) which the array column may have.&lt;/p&gt;

&lt;p&gt;Here are three ways to do different kinds of searches.&lt;/p&gt;

&lt;p&gt;1) Use ANY operator when searching with one value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM mytable WHERE 'first_type' = ANY(types_column);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;2) Go with the &lt;a href="https://www.postgresql.org/docs/current/functions-array.html"&gt;"contains" operator&lt;/a&gt; (“@&amp;gt;”) when you look for a specific set of values (the order of values doesn’t matter):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM mytable WHERE types_column @&amp;gt; '{"first_type", "second_type"}';
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The values &lt;code&gt;“first_type”&lt;/code&gt; and &lt;code&gt;“second_type"&lt;/code&gt; must be in the &lt;code&gt;types_column&lt;/code&gt; column for a record to be selected.&lt;/p&gt;

&lt;p&gt;3) Whenever you need to search any values that a column may have - use the &lt;a href="https://www.postgresql.org/docs/current/functions-array.html"&gt;"overlap" operator&lt;/a&gt; (“&amp;amp;&amp;amp;”)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM mytable WHERE types_column &amp;amp;&amp;amp; '{"first_type", "second_type"}';
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;One of the values &lt;code&gt;“first_type”&lt;/code&gt; or &lt;code&gt;“second_type"&lt;/code&gt; must be in the &lt;code&gt;types_column&lt;/code&gt; column for a record to be selected.&lt;/p&gt;

</description>
      <category>postgres</category>
    </item>
  </channel>
</rss>
