title: REPLACE INTO with SELECT condition
body: <p>I have table: </p>
  
  <pre><code>mysql&gt; desc dialog;
  +-----------+------------------+------+-----+---------+----------------+
  | Field     | Type             | Null | Key | Default | Extra          |
  +-----------+------------------+------+-----+---------+----------------+
  | id        | int(11)          | NO   | PRI | NULL    | auto_increment |
  | uid1      | int(11)          | NO   | MUL | NULL    |                |
  | uid2      | int(11)          | NO   | MUL | NULL    |                |
  | mid       | int(11)          | NO   |     | NULL    |                |
  | anonym_id | int(10) unsigned | NO   |     | NULL    |                |
  +-----------+------------------+------+-----+---------+----------------+
  </code></pre>
  
  <p>and the same one: </p>
  
  <pre><code>mysql&gt; desc dialogs;
  +-----------+------------------+------+-----+---------+----------------+
  | Field     | Type             | Null | Key | Default | Extra          |
  +-----------+------------------+------+-----+---------+----------------+
  | id        | int(11)          | NO   | PRI | NULL    | auto_increment |
  | uid1      | int(11)          | NO   | MUL | NULL    |                |
  | uid2      | int(11)          | NO   | MUL | NULL    |                |
  | mid       | int(11)          | NO   |     | NULL    |                |
  | anonym_id | int(10) unsigned | NO   | MUL | NULL    |                |
  +-----------+------------------+------+-----+---------+----------------+
  </code></pre>
  
  <p>The 1-st one - dialog has a duplicate values which I don't need in the 2-nd - dialogs, the structure is the same, except unique index in dialogs table:</p>
  
  <pre><code>mysql&gt; show index from dialogs;
  +---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  | Table   | Non_unique | Key_name  | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
  +---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  | dialogs |          0 | PRIMARY   |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
  | dialogs |          0 | uid1_uid2 |            1 | uid1        | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
  | dialogs |          0 | uid1_uid2 |            2 | uid2        | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
  | dialogs |          1 | uid2      |            1 | uid2        | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
  | dialogs |          1 | anonym_id |            1 | anonym_id   | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
  +---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  5 rows in set (0.00 sec)
  </code></pre>
  
  <p>I've found the way to insert into dialogs from dialog without duplicate key, but one:</p>
  
  <pre><code>REPLACE INTO dialogs SELECT * FROM dialog;
  </code></pre>
  
  <p>Works like a charm, but - I need either to test IF uid2>uid1 THEN switch them vice-versa and insert uid1=uid2 and uid2=uid1. Is there a solution to do almost the same, but with this clause with 1 query? 
  I`d tried something like, but this query failed to execute: </p>
  
  <pre><code>REPLACE INTO dialogs 
      SELECT IF uid1&lt;uid2 THEN * 
             ELSE id, uid2, uid1, mid, anonym_id 
  END FROM dialog;
  </code></pre>
  
  <p>If so - please, post an example - I`ll try it.</p>
  
  <p>Thx.</p>