mysql deduplication, cross-table update, cross-table delete

1. Deduplication
1. Query duplicate records
CREATE TABLE push_log_full_2013_10_30_tmp
SELECT * FROM `push_log_full`
WHERE time BETWEEN FROM_DAYS(TO_DAYS(NOW()) - 1) AND FROM_DAYS(TO_DAYS(NOW()))
AND (imsi, andriodid, time) IN (
SELECT imsi, andriodid, time FROM `push_log_full`
WHERE time BETWEEN FROM_DAYS(TO_DAYS(NOW()) - 1) AND FROM_DAYS(TO_DAYS(NOW()))
GROUP BY imsi, andriodid, time
HAVING COUNT(*) > 1)

Check out the duplicate records and put them in a temporary table.

2
Delete the duplicate data in the push_log_full table.
3
Re-import the data of the temporary table into push_log_full.

2. Update across tables
Method 1:
UPDATE product p, productPrice pp
SET pp.price = pp.price * 0.8
WHERE p.productId = pp.productId
AND p.dateCreated < '2004-01-01'


Law two:
UPDATE product p
INNER JOIN productPrice pp
ON p.productId = pp.productId
SET pp.price = pp.price * 0.8
WHERE p.dateCreated < '2004-01-01'


Method 3 multi-table update:
UPDATE product p
INNER JOIN productPrice pp
ON p.productId = pp.productId
SET pp.price = pp.price * 0.8,
p.dateUpdate = CURDATE()
WHERE p.dateCreated < '2004-01-01'


3. Cross-table delete
After Mysql4.0, mysql began to support cross-table delete.
Mysql can delete records in multiple tables at the same time in one sql statement, or delete records in a certain table according to the relationship between multiple tables.
Suppose we have two tables: Product table and ProductPrice table. The former has the basic information of the Product, and the latter has the price of the Product.
The first way to delete across tables is to not use join. When deleting, specify multiple tables separated by commas to delete. The following sql statement:
DELETE p.*, pp.*
FROM product p, productPrice pp
WHERE p.productId = pp.productId
AND p.created < '2004-01-01'


The second way to delete across tables is to use inner join to specify the relationship between the two tables in join, as follows:

DELETE p.*, pp.*
FROM product p
INNER JOIN productPrice pp
ON p.productId = pp.productId
WHERE p.created < '2004-01-01'


Cross-table deletion can also use left join. For example, we want to delete all Product table records that have no records in the ProductPrice table. The following sql statement:

DELETE p.*
FROM product p
LEFT JOIN productPrice pp
ON p.productId = pp.productId
WHERE pp.productId is null

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326679393&siteId=291194637