用SELECT条件替换INTO

时间:2014-05-17 11:15:31

标签: mysql sql

我有桌子:

mysql> 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    |                |
+-----------+------------------+------+-----+---------+----------------+

和同一个人:

mysql> 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    |                |
+-----------+------------------+------+-----+---------+----------------+

第一个对话框有一个重复的值,在第二个对话框中我不需要,结构是相同的,除了对话框表中的唯一索引:

mysql> 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)

我找到了从没有重复键的对话框插入对话框的方法,但只有一个:

REPLACE INTO dialogs SELECT * FROM dialog;

像魅力一样工作,但是 - 我需要测试IF uid2> uid1那么切换它们反之亦然并插入uid1 = uid2和uid2 = uid1。是否有一个解决方案几乎相同,但这个子句有1个查询? 我试过类似的东西,但是这个查询无法执行:

REPLACE INTO dialogs 
    SELECT IF uid1<uid2 THEN * 
           ELSE id, uid2, uid1, mid, anonym_id 
END FROM dialog;

如果是这样 - 请发一个例子 - 我会试试。

THX。

1 个答案:

答案 0 :(得分:0)

而不是REPLACE INTO会增加你的id,你可以use INSERT IGNORE将它们留在原处(如果已经存在)。至于切换uid1, uid2的值,请将值设置为CASE

INSERT IGNORE INTO dialogs (id, uid1, uid2, mid, anonym_id)
  SELECT
    id,
    /* First prefer uid1, then the opposite for uid2 */
    CASE WHEN uid1 < uid2 THEN uid1 ELSE uid2 END,
    CASE WHEN uid1 < uid2 THEN uid2 ELSE uid1 END,
    mid,
    anonym_id
  FROM dialog

A little more on the difference between REPLACE and INSERT IGNORE ...