Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should to union both columns and then filter for distinct values:</p> <pre><code>select distinct T.from_to from ( select `from` as from_to from messages union select `to` as from_to from messages ) T </code></pre> <p>if you really need all in a comma separate string, use <a href="http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat" rel="nofollow">GROUP_CONCAT([DISTINCT]</a> aggregation function.</p> <p><strong>EDITED</strong>:</p> <p>You should mark as solution Gerald answer. After test union operator and reread <a href="http://dev.mysql.com/doc/refman/5.0/en/union.html" rel="nofollow">mysql union operator documentation</a>, by default, mysql filter for distinct values:</p> <pre><code>mysql&gt; create table ta( a int ); Query OK, 0 rows affected (0.05 sec) mysql&gt; insert into ta values (1),(1),(2); Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql&gt; select * from ta -&gt; union -&gt; select * from ta; +------+ | a | +------+ | 1 | | 2 | +------+ 2 rows in set (0.00 sec) </code></pre> <p>then, the <strong>final query</strong> is:</p> <pre><code> select `from` as from_to from messages union distinct select `to` as from_to from messages </code></pre> <p>Notice that <code>distinct</code> is not mandatory.</p> <p>Only if you need a comma sparate string the first solution is necessary:</p> <pre><code>select distinct GROUP_CONCAT( distinct T.from_to from ) ( select `from` as from_to from messages union select `to` as from_to from messages ) T </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload