Answer the question
In order to leave comments, you need to log in
Data movement and grouping?
Good morning!
There are two tables:
Headings:
id | name_section |
---|
id | id_section | name_goods |
---|
id | id_goods | id_section |
---|
// ищем одинаковые<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`<>t2.`id_goods`<br/>
ORDER BY `t1`.`name_goods`<br/>
Answer the question
In order to leave comments, you need to log in
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;
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
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 questionAsk a Question
731 491 924 answers to any question