Update multiple columns in MySql with results from a SELECT statement - mysql

Background: I have an orders table that contains address columns. I would like to update these with randomly picked addresses taken from a temporary table
Both tables contain address, address1, city and postcode columns
I was thinking the query would be something like:
UPDATE orders (address, address1, city, postcode)
VALUE
(SELECT address, address1, city, postcode
FROM addresses
ORDER BY RAND()
LIMIT 1)
Edit: Note that it needs update all rows with different values.

UPDATE orders
JOIN ( SELECT address, address1, city, postcode
FROM addresses
ORDER BY RAND()
LIMIT 1
) AS newdata
SET orders.address = newdata.address,
orders.address1 = newdata.address1,
orders.city = newdata.city,
orders.postcode = newdata.postcode
WHERE orders.id = 72;

Related

Select record from one table and add into table 2, update last inserted ID into table1

I have two different tables tbl_address and tbl_support. I want to select address, postcode from tbl_support, and insert into into tbl_address and want to update address_id in tbl_support with last inserted id. How do I write query.
tbl_address = id, address, postcode, country.
tbl_support = id, address_id ,name, address,postcode,
if your column id field in address table autoincrement try this:
insert into tbl_address select address, postcode from tbl_support;
then you can update tbl_support if address and postcode are unique set.
update tbl_support s set s.address_id=(select id from tbl_address a where
a.address=s.address and a.postcode = s.postcode)
you need to execute to two separate queries. first you insert all tbl_support record in tbl_address table.then you need to execute second query where you update address_id through a matching tbl_address address,postcode and tbl_support address, zip code.like below query.
INSERT INTO tbl_address (
address,
postcode)
SELECT address,
postcode
FROM `tbl_support`;
UPDATE `tbl_support` SET address_id = (SELECT id FROM `tbl_address`
WHERE tbl_support.address= tbl_address.address
AND tbl_support.postcode= tbl_address.postcode)

MySQL - Update with COUNT, is it possible?

I'm attempting to update a MySQL table to show column name 'processed' as '2' if there is duplicate entries for 'name' and 'address_1', but it's not working - as usual I think I'm just being a bit of a moron..
Here's what I'm trying
UPDATE `records`
SET `processed`='2', `count` = (SELECT COUNT(`user`)
FROM `records`
WHERE `name`<>''
AND `address_1`<>'')
WHERE `count`=> '1';
Basically, if there's more than one 'name' and 'address_1' then the 'processed' field needs updating to '2'..
You could use a query like this one to return duplicated names and addresses:
SELECT name, address_1, COUNT(*) cnt
FROM records
GROUP BY name, address_1
HAVING COUNT(*)>1
and then join this query to the records table, and update the column processed to 2 where the join succeeds:
UPDATE
records INNER JOIN (SELECT name, address_1, COUNT(*) cnt
FROM records
GROUP BY name, address_1
HAVING COUNT(*)>1) duplicates
ON records.name = duplicates.name
AND records.address_1=duplicates.address_1
SET
`processed`='2',
`count` = duplicates.cnt
WHERE
records.`name`<>''
AND records.`address_1`<>''

MySQL - Add duplicate rows to archive table, then delete duplicate rows

I've been researching the proper way to find duplicate rows based on specific fields for days now. I think I need a little more help -
SELECT *
FROM enrollees
INNER JOIN (SELECT first_name, last_name, address1, city, state, zip, program_instance_id, MIN(id) AS MinId, COUNT(id) AS count FROM enrollees GROUP BY first_name, last_name, address1, city, state, zip, program_instance_id) b
ON enrollees.first_name = b.first_name
AND enrollees.last_name = b.last_name
AND enrollees.address1 = b.address1
AND enrollees.city = b.city
AND enrollees.state = b.state
AND enrollees.zip = b.zip
AND count > 1
AND enrollees.program_instance_id = b.program_instance_id
AND enrollees.id != MinId;
The goal is to take the duplicates and put them in an archive table (enrollees_duplicates), then delete the duplicates from the live table (enrollees). I tried writing one query to find and insert the duplicate rows but it gives me the following error:
"Column count doesn't match value count at row 1"
The query I tried using is:
INSERT INTO enrollees_duplicates (SELECT *
FROM enrollees
INNER JOIN (SELECT first_name, last_name, address1, city, state, zip, program_instance_id, MIN(id) AS MinId, COUNT(id) AS count FROM enrollees GROUP BY first_name, last_name, address1, city, state, zip, program_instance_id) b
ON enrollees.first_name = b.first_name
AND enrollees.last_name = b.last_name
AND enrollees.address1 = b.address1
AND enrollees.city = b.city
AND enrollees.state = b.state
AND enrollees.zip = b.zip
AND count > 1
AND enrollees.program_instance_id = b.program_instance_id
AND enrollees.id != MinId);
I assume it is because I'm not retrieving all of the columns in the INNER JOIN select? If that's the case, wouldn't it still throw the same error if I changed it to SELECT * (with the MinId and count additions) because there would be two extra columns that don't exist in the new table?
Is there any way to do all of the work with an SQL query without having to SELECT the duplicates, store them in a PHP array, and then use another SQL query to pull each row, INSERT it into the duplicate table, and then another SQL query to delete the duplicate row.
My intention was to use two queries. One to insert all duplicate rows into the archive table and another to delete the duplicate rows. If it could, somehow, be made into one query that finds the duplicates, inserts them into the archive table, and then deletes them - all in one run, that would be even better.
Being new to this field, Any help or guidance would be appreciated.
"Column count doesn't match value count at row 1"
Tables enrollees_duplicates and enrollees have diffrent structure.
Might be better to use ON DELETE TRIGGER ? (http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html).
The solution to my problem is that when my first select was just '*', it was adding the two additional columns (MinId, count) to the result which made the column count different. By only grabbing the results of the 'enrollees' table and not the additional parameters of the subquery too, it corrects the column difference.
INSERT INTO enrollees_duplicates (SELECT enrollees.*
FROM enrollees
INNER JOIN (SELECT first_name, last_name, address1, city, state, zip, program_instance_id, MIN(id) AS MinId, COUNT(id) AS count FROM enrollees GROUP BY first_name, last_name, address1, city, state, zip, program_instance_id) b
ON enrollees.first_name = b.first_name
AND enrollees.last_name = b.last_name
AND enrollees.address1 = b.address1
AND enrollees.city = b.city
AND enrollees.state = b.state
AND enrollees.zip = b.zip
AND count > 1
AND enrollees.program_instance_id = b.program_instance_id
AND enrollees.id != MinId);

MySQL: What is the safest way to remove duplicate rows from a MySQL TABLE?

I have a MySQL TABLE. It contains mailing addresses we get from a data feed. But there are no customer records for the mailing addresses, so I don't have an easy way to match a customer record as a key to see if it exists already in the master TABLE. So I've decided to have the new daily data feed added to the master TABLE and then remove duplicates.
What is the safest way to remove duplicates? Obviously, I want to ignore the ID column field. But how do I do this for the following fields:
company_name
contact_name
address1
address2
address3
city
state
zipcode
phone_number
email_address
What if I rebuild the MySQL TABLE to include ALTER TABLE with UNIQUE KEY, would that be safe? For example:
ALTER TABLE people ADD UNIQUE KEY (company_name,contact_name,address1,address2,address3,city,state,zipcode,phone_number,email_address)
Would the above safely prevent duplicated records from being INSERTed to begin with?
Thanks!
This is simplest Query you can use
Choose Max or min based on your requirement.
DELETE
FROM MyTable
WHERE ID NOT IN
(
SELECT MAX(ID)
FROM MyTable
GROUP BY DuplicateColumn1, DuplicateColumn2, DuplicateColumn3)
Thanks
DELETE a FROM test a
LEFT JOIN
(
SELECT MIN(id) AS id, company_name, contact_name, address1, address2, address3, city, state, zipcode, phone_number, email_address
FROM test
GROUP BY company_name, contact_name, address1, address2, address3, city, state, zipcode, phone_number, email_address
) b ON a.id = b.id AND a.company_name = b.company_name AND a.contact_name = b.contact_name AND a.address1 = b.address1 AND a.address2 = b.address2 AND a.address3 = b.address3 AND a.city = b.city AND a.state = b.state AND a.zipcode = b.zipcode AND a.phone_number = b.phone_number AND a.email_address = b.email_address
WHERE b.id IS NULL

Repeating column names when outputting a query

Is it possible to repeat the names of a database table's columns when executing a query whenever the GROUP BY info change? My query is group by a column.
SELECT name.fname as First_Name,
name.sales as Sales,
name.city as City
FROM name
GROUP BY city
ORDER BY name.fname;
I think you want this:
SELECT name.fname as First_Name,
name.sales as Sales,
name.city as City
FROM name
ORDER BY city, fname;