MYSQL Query generating multiplication of existing value in table - mysql

i am trying to do a mysql query to update a table with some values in order of ID, i have sucess in the first time (generate 38 result row) but when i execute this again, this multiplicate the existing value in the table (38x38 = 1444 row result) repeting the already existing product_id in new ID rows.....
i want to insert to this table, only unique product_id from the another target table, and group by the ID [1,2,3,4,5, and dont repeat this too]
the code:
/* add all eligible customers to temporary table */
INSERT INTO `temp_wp_woocommerce_subscription_downloads`
(`product_id`, `subscription_id`)
SELECT posts.ID AS product_id, downn.subscription_id AS subscription_id
FROM wp_posts posts
JOIN wp_woocommerce_subscription_downloads downn
WHERE posts.post_type = 'product'
AND posts.post_status = 'publish'
;
in the print you can see the generated issue:
(https://prntscr.com/s2zhil)
EDIT: Work perfect with the code from the #GordonLinoff answer,but after run and add a new product and run the query again the ID is dont by a order (appear to "jump/burn some ids", how i order by unique id's too? and as you can see :
http://prntscr.com/s31rrq

This addresses the original version of the question.
You can add a unique index on productid and then use on duplicate key.
alter table temp_wp_woocommerce_subscription_downloads
add constraint unq_temp_wp_woocommerce_subscription_downloads_product_id
unique(product_id);
INSERT INTO temp_wp_woocommerce_subscription_downloads(`product_id`, `subscription_id`)
SELECT p.ID AS product_id, d.subscription_id AS subscription_id
FROM wp_posts posts p JOIN
wp_woocommerce_subscription_downloads d
ON p.post_type = 'product' AND p.post_status = 'publish'
ON DUPLICATE KEY UPDATE subscription_id = d.subscription_id;
That said, I suspect that you might also be missing a JOIN condition on the table -- say by post or product.

Related

What sql query to use for only deleting duplicate results for wp_comments table?

I need to finish the select query below. The query shows me the count of comments with the same comment_id.I just ultimately want to delete the duplicates and leave the non duplicates alone.This is a wordpress database
screenshot of my current query results
SELECT `comment_ID`, `comment_ID`, count(*) FROM `wp_comments` GROUP BY `comment_ID` HAVING COUNT(*) > 1 ORDER BY `count(*)` ASC
example of 2 entries I need to delete one
First back up your bad table in case you goof something up.
CREATE TABLE wp_commments_bad_backup SELECT * FROM wp_comments;
Do you actually have duplicate records here (duplicate in all columns) ? If so, try this
CREATE TABLE wp_comments_deduped SELECT DISTINCT * FROM wp_comments;
RENAME TABLE wp_comments TO wp_comments_not_deduped;
RENAME TABLE wp_comments_deduped TO wp_comments;
If they don't have exactly the same contents and you don't care which contents you keep from each pair of duplicate rows, try something like this:
CREATE TABLE wp_comments_deduped
SELECT comment_ID,
MAX(comment_post_ID) comment_post_ID,
MAX(comment_author) comment_author,
MAX(comment_author_email) comment_author_email,
MAX(comment_author_url) comment_author_url,
MAX(comment_author_IP) comment_author_IP,
MAX(comment_date) comment_date,
MAX(comment_date_gmt) comment_date_gmt,
MAX(comment_content) comment_content,
MAX(comment_karma) comment_karma,
MAX(comment_approved) comment_approved,
MAX(comment_agent) comment_agent,
MAX(comment_type) comment_type,
MAX(comment_parent) comment_parent,
MAX(user_id) user_id
FROM wp_comments
GROUP BY comment_ID;
RENAME TABLE wp_comments TO wp_comments_not_deduped;
RENAME TABLE wp_comments_deduped TO wp_comments;
Then you'll need to doublecheck whether your deduplicating worked:
SELECT comment_ID, COUNT(*) num FROM wp_comments GROUP BY comment_ID;
Then, once you're happy with it, put back WordPress's indexes.
Pro tip: Use a plugin like Duplicator when you migrate from one WordPress setup to another; its authors have sorted out all this data migration for you.
I would recommand add a unique key to the table make it auto incremental call it tempId , so you would be able to to distinguish between one duplicate set, use below query to remove duplicate copies and at the end remove that '`tempid' column:
DELETE FROM `wp_comments`
WHERE EXISTS (
SELECT `comment_ID` , MIN(`tempid`) AS `tempid`
FROM `wp_comments` as `dups`
GROUP BY `comment_ID`
HAVING
COUNT(*) > 1
AND `dups`.`comment_ID` = `wp_comments`.`comment_ID`
AND `dups`.`tempid` = `wp_comments`.`tempid`
)
I'm not clear on why there appear to be two different fields both named 'column_ID' from the same table, but I believe this will delete only the first of the two identical records. Before running a DELETE statement, however, be sure to make a backup of the original table.
DELETE
TOP 1 *
FROM
'wp_comments'
WHERE
comment_ID IN
(
SELECT
comment_ID,
r,
(comment_ID + '_' + r) AS unique
FROM
(
SELECT
`comment_ID`,
`comment_ID`,
RANK() OVER (PARTITION BY 'comment_id' ORDER BY 'comment_id') AS r
FROM
'wp_comments'
)
WHERE
r>1
)

Unable to create a temporary table from a select query with multiple joins. Throws 'Duplicate column name 'id'

This is the query that is causing the problem:
CREATE TEMPORARY TABLE fetchedCropVariety ENGINE = MEMORY
SELECT *
, hz.zoneName
, i.fullImageUrl
, i.previewImageUrl
, u.userName
FROM seedrecord s
LEFT
JOIN HardinessZone hz
ON hz.id = s.hardiness_zone_id
JOIN image i
ON s.id = i.seedrecord_id
JOIN members u
ON s.FK_USER = u.id
WHERE s.id = 1
AND s.deleted = FALSE;
When i execute this query it throws the error Error Code: 1060. Duplicate column name 'id'
There are no duplicate columns named id in the table seedrecord so this is caused by one or more id columns from the other tables that are only used to join them in order to retrieve zoneName and fullImageUrl.
When i remove *, from the query the query does run but all the fields from seedrecord are omitted. It must be the id column from seedrecord that is clashing somehow with the id columns from hardinessZone and members but I am not trying to include the id fields of those 2 tables into the temporary table i want to create so this error is really puzzeling me.
Can anyone tell me what i can do to stop this error from occurring?
Thank you
don't make
select *
It will take all clomuns that you have in your From Clause
And you have the at least two Columns that are namend id
MAke instead
S.*,u.*
and so on

Delete post with smallest id SQL

Delete from post
where id_post
in
(
select MIN(id_post)
from post
where id_owner='2'
)
Returns:
"You can't specify target table 'post' for update in FROM clause"
What am I doing wrong?
The problem is that MySQL, if you're doing an UPDATE/INSERT/DELETE on a table, you can't reference that table in an inner query (you can however reference a field from that outer table...)
The solution is to replace the instance of post in the sub-query with (select MIN(id_post)
from post
where id_owner='2' ), like this
Delete from post
where id_post
in
(
select id_post
from (select MIN(id_post)
from post
where id_owner='2') as A
)
How about using ORDER BY and LIMIT?
Delete p
from post p
where id_owner = 2
order by id_post
limit 1;
Note: Don't use single quotes around numeric constants. I am guessing id_owner is numeric.

Mysql Update statement to delete duplicates

I would like to make an update on my unique id like replacing white space, but on that case the update statement breaks because of redundant s I tried to make an update as it follows but
wrong syntax
UPDATE __cat_product
SET product_id = REPLACE(product_id,' ','')
ON DUPLICATE KEY DELETE product_id
WHERE product_id LIKE "%A %"
how to do this in the right way?
There is no ON DUPLICATE KEY syntax that is part of an UPDATE statement.
I'm having difficulty figuring out what you actually want to do.
You want to update a column in a table, to remove all space characters. And when you tried to run that statement, the statement encountered an error like Duplicate entry 'foo' for key 'PRIMARY'.
Assuming that product_id is varchar, try this on for size:
UPDATE __cat_product t
JOIN ( SELECT MAX(s.product_id) AS product_id
, REPLACE(s.product_id,' ','')
FROM __cat_product s
WHERE s.product_id LIKE '%A %'
AND NOT EXISTS ( SELECT 1
FROM __cat_product d
WHERE d.product_id = REPLACE(s.product_id,' ','')
)
GROUP BY REPLACE(s.product_id,' ','')
) r
ON r.product_id = t.product_id
SET t.product_id = REPLACE(t.product_id,' ','')
The inline view aliased as r gets the values of product_id that can actually be updated, because the "updated" product_id value doesn't already exist in the table. And the GROUP BY ensures that we're only getting one product_id per targeted replacement value.
There may be rows in the table that still match the predicate product_id LIKE '%A %'. The spaces cannot be removed from product_id column on those rows without throwing a duplicate key exception.
A separate DELETE statement could be used to remove those rows.

Using var into an insert update request does not work

I try to insert values into a table using following request :
INSERT IGNORE itemmaster
(
SKU,
product_id
)
SELECT
#massimport_SKU := main.SKU,
#massimport_product_id := prd.entity_id
FROM itemmaster AS main
INNER JOIN product_entity AS prd ON prd.sku = main.SKU
ON DUPLICATE KEY UPDATE product_id = #massimport_product_id
SKU is a unique key.
The problem is the value of product_id is always the same id. If I only execute the select, product_id are different but after insert, only one value in product_id column. I think this is a problem with var #massimport_product_id cause if I useON DUPLICATE KEY UPDATE product_id = prd.entity_id instead the request work perfectly.
But cause it's an automatic generated request who work well in all other case, I hope somebody coul me explain why this append.
Thx
Don't use session variables, that can't work.
Use this:
INSERT IGNORE itemmaster( SKU, product_id )
SELECT main.SKU, prd.entity_id
FROM itemmaster AS main
INNER JOIN product_entity AS prd ON prd.sku = main.SKU
ON DUPLICATE KEY UPDATE product_id = prd.entity_id
;
Demo --> http://www.sqlfiddle.com/#!2/592187/1
However INNER JOIN in this query always returns records (values of SKU column), that already exist in itemmaster table, so INSERT is useles in this case, and the same can be done simpler, using multitable update:
UPDATE itemmaster main
JOIN product_entity AS prd ON prd.sku = main.SKU
SET main.product_id = prd.entity_id;
Demo: http://www.sqlfiddle.com/#!2/762eb4/1