MySQL去掉重複記錄

來源:互聯網
上載者:User

mysql> select * from test;
+--------+------+-----------------+---------+-------------------------------+
| p_id   | g_id | p_name          | p_price | p_desc                        |
+--------+------+-----------------+---------+-------------------------------+
| Beef1  | 1003 | Beef (1 stick)  |    2.50 | Beef, pink, single stick      |
| Beef2  | 1003 | Beef (5 sticks) |   11.00 | Beef, pink, pack of 10 sticks |
| CAB    | 1003 | Cable           |    4.42 | Cable, one size fits all      |
| FB     | 1003 | Seed            |   11.50 | L bag                         |
| FU1    | 1002 | Fuses           |    3.42 | 1 dozen, extra long           |
| IRON01 | 1001 | .5 kg iron      |    4.99 | .5 kg iron, black             |
| IRON02 | 1001 | 1 kg iron       |    8.99 | 1 kg iron, black              |
| IRON03 | 1001 | 2 kg iron       |   13.99 | 2 kg iron, black              |
| JP1100 | 1005 | Jet 1000        |   36.00 | Jet 1000, single use          |
| JP2000 | 1005 | Jet 2000        |   51.00 | Jet 2000, multi-use           |
| OIL1   | 1002 | Oil bottle      |    7.99 | Oil bottal, red               |
| PO     | 1003 | Potatoes        |    2.50 | Potatoes                      |
| SB     | 1003 | Safebox         |   51.00 | Safe with combination lock    |
| TP     | 1003 | Toothpaste      |   12.00 | Toothpaste, 10 pack           |
| Beef1  | 1003 | Beef (1 stick)  |    2.50 | Beef, pink, single stick      |
| Beef2  | 1003 | Beef (5 sticks) |   11.00 | Beef, pink, pack of 10 sticks |
| CAB    | 1003 | Cable           |    4.42 | Cable, one size fits all      |
| FB     | 1003 | Seed            |   11.50 | L bag                         |
| FU1    | 1002 | Fuses           |    3.42 | 1 dozen, extra long           |
| IRON01 | 1001 | .5 kg iron      |    4.99 | .5 kg iron, black             |
| IRON02 | 1001 | 1 kg iron       |    8.99 | 1 kg iron, black              |
| IRON03 | 1001 | 2 kg iron       |   13.99 | 2 kg iron, black              |
| JP1100 | 1005 | Jet 1000        |   36.00 | Jet 1000, single use          |
| JP2000 | 1005 | Jet 2000        |   51.00 | Jet 2000, multi-use           |
| OIL1   | 1002 | Oil bottle      |    7.99 | Oil bottal, red               |
| PO     | 1003 | Potatoes        |    2.50 | Potatoes                      |
| SB     | 1003 | Safebox         |   51.00 | Safe with combination lock    |
| TP     | 1003 | Toothpaste      |   12.00 | Toothpaste, 10 pack           |
| Beef1  | 1003 | Beef (1 stick)  |    2.50 | Beef, pink, single stick      |
| Beef2  | 1003 | Beef (5 sticks) |   11.00 | Beef, pink, pack of 10 sticks |
| CAB    | 1003 | Cable           |    4.42 | Cable, one size fits all      |
| FB     | 1003 | Seed            |   11.50 | L bag                         |
| FU1    | 1002 | Fuses           |    3.42 | 1 dozen, extra long           |
| IRON01 | 1001 | .5 kg iron      |    4.99 | .5 kg iron, black             |
| IRON02 | 1001 | 1 kg iron       |    8.99 | 1 kg iron, black              |
| IRON03 | 1001 | 2 kg iron       |   13.99 | 2 kg iron, black              |
| JP1100 | 1005 | Jet 1000        |   36.00 | Jet 1000, single use          |
| JP2000 | 1005 | Jet 2000        |   51.00 | Jet 2000, multi-use           |
| OIL1   | 1002 | Oil bottle      |    7.99 | Oil bottal, red               |
| PO     | 1003 | Potatoes        |    2.50 | Potatoes                      |
| SB     | 1003 | Safebox         |   51.00 | Safe with combination lock    |
| TP     | 1003 | Toothpaste      |   12.00 | Toothpaste, 10 pack           |
+--------+------+-----------------+---------+-------------------------------+
42 rows in set (0.00 sec)

mysql> create table tmp as (select * from test group by p_id having count(*)=1);  //建立暫存資料表,複製表結構
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> insert into tmp(select * from test group by p_id having count(*)>1); //複製資料
Query OK, 14 rows affected (0.00 sec)
Records: 14  Duplicates: 0  Warnings: 0

mysql> truncate table test; 
Query OK, 0 rows affected (0.00 sec)

mysql> insert into test select * from tmp;
Query OK, 14 rows affected (0.00 sec)
Records: 14  Duplicates: 0  Warnings: 0

mysql> select * from tmp;
+--------+------+-----------------+---------+-------------------------------+
| p_id   | g_id | p_name          | p_price | p_desc                        |
+--------+------+-----------------+---------+-------------------------------+
| Beef1  | 1003 | Beef (1 stick)  |    2.50 | Beef, pink, single stick      |
| Beef2  | 1003 | Beef (5 sticks) |   11.00 | Beef, pink, pack of 10 sticks |
| CAB    | 1003 | Cable           |    4.42 | Cable, one size fits all      |
| FB     | 1003 | Seed            |   11.50 | L bag                         |
| FU1    | 1002 | Fuses           |    3.42 | 1 dozen, extra long           |
| IRON01 | 1001 | .5 kg iron      |    4.99 | .5 kg iron, black             |
| IRON02 | 1001 | 1 kg iron       |    8.99 | 1 kg iron, black              |
| IRON03 | 1001 | 2 kg iron       |   13.99 | 2 kg iron, black              |
| JP1100 | 1005 | Jet 1000        |   36.00 | Jet 1000, single use          |
| JP2000 | 1005 | Jet 2000        |   51.00 | Jet 2000, multi-use           |
| OIL1   | 1002 | Oil bottle      |    7.99 | Oil bottal, red               |
| PO     | 1003 | Potatoes        |    2.50 | Potatoes                      |
| SB     | 1003 | Safebox         |   51.00 | Safe with combination lock    |
| TP     | 1003 | Toothpaste      |   12.00 | Toothpaste, 10 pack           |
+--------+------+-----------------+---------+-------------------------------+
14 rows in set (0.00 sec)

mysql>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.