E
E
enchikiben2012-01-17 07:26:32
MySQL
enchikiben, 2012-01-17 07:26:32

Data movement and grouping?

Good morning!
There are two tables:
Headings:

idname_section

Products:
idid_sectionname_goods

And it so happened that the goods are beginning to be duplicated, in order to avoid data redundancy, I decided to insert an intermediate link table.
Connections:
idid_goodsid_section

Now we need to somehow move the existing goods in the connection, delete the duplicates and update the records where the duplicates were deleted. Now I just came up with the following:
1. Move all products in a connection without repetitions (temporarily create a product name field in it).
2. Select all duplicate products (here you need to use the link table) from the table of products and enter them in connection with the product number from the link table, and leave the section number as the old one.
3. Remove duplicates and headings column from the product table.
Please let me know if anyone has experience with this kind of work.
// ищем одинаковые<br/>
SELECT <br/>
t1.`id_section`,<br/>
t1.`id_goods`,<br/>
t1.`name_goods`,<br/>
t2.`id_goods`,<br/>
t2.`id_section`<br/>
FROM im_goods as t1, im_goods as t2 <br/>
WHERE <br/>
t1.`name_goods`=t2.`name_goods` AND t1.`id_goods`&lt;&gt;t2.`id_goods`<br/>
ORDER BY `t1`.`name_goods`<br/>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Shedal, 2012-01-17
@EnChikiben

CREATE TABLE im_goods_new (
   id int(10) unsigned zerofill NOT NULL auto_increment,
   name varchar(255) NOT NULL,
   PRIMARY KEY  (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

INSERT INTO im_goods_new
SELECT DISTINCT name_goods
FROM im_goods;

INSERT INTO im_goods_sections (id_goods, id_section)
SELECT DISTINCT new.id, old.id_section
FROM im_goods_new new
JOIN im_goods old
  ON new.name = old.name_goods;
  
DROP TABLE im_goods;

RENAME TABLE im_goods_new TO im_goods;

I
Ilya Plotnikov, 2012-01-17
@ilyaplot

If you only need to delete duplicates, then A update of the record ... Well, here I will not tell you, unfortunately.
DELETE t1 FROM table t1, table t2 WHERE t1.name=t2.name AND t1.ID > t2.ID

E
egorinsk, 2012-01-17
@egorinsk

If you need to prohibit the creation of products with the same names, you need to make a UNIQUE INDEX on the name_goods field. To prevent the index from becoming a huge ugly slowing monster, it must be declared something like this (limit the size of the indexed substring):
ALTER TABLE im_goods ADD UNIQUE INDEX ix_name ( name_goods(10) );
Then the database will not allow you to insert 2 products with the same name (make sure that this is not a surprise later).
Although, maybe I misunderstood you.
PS I also advise you to correctly name the fields next time so that those who understand English do not warp the eyes: product = product, name = product_name, product id = product_id, heading = category. Suddenly, for example, you will make a store for Americans, it will come in handy.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question