I have some records in tables A with fields i.e firstname surname, lastname, school, dob
I have another table B with some records and fields i.e firstname, surname, address, club,
I'd like to use the firstname and surname in table B to check if the record exist in table A if it doesnt it should append the record to table A.
I would be glad if you can help me with this
This SQL should do this. Basically, insert from table_b, records with firstname and lastname not existing in table_a.
INSERT INTO table_a (firstname, lastname, address, club)
(
SELECT DISTINCT firstname, lastname, address, club FROM table_b
WHERE (firstname, lastname) NOT IN (SELECT firstname, lastname FROM table_a)
)
Try this query -
INSERT INTO table_a(firstname, lastname)
SELECT b.firstname, b.lastname FROM table_b b
LEFT JOIN table_a a ON b.firstname = a.firstname AND b.lastname = a.lastname
WHERE a.firstname IS NULL AND a.lastname IS NULL;
Related
I have two tables named tbl_1 (columns: id, firstname, lastname) and tbl_2 (columns: id, email, phone).
I want to fetch the data of certain student, for example abc with id = 2). How can I do this in MySQL?
Note: I don't want to fetch all students data.
This one is for all the data:
SELECT
id, firstname, lastname, email, phone
FROM tbl_1, tbl_2
WHERE tbl_1.id = tbl_2.id
Just add the selected id. Ex:
SELECT id, firstname, lastname, email, phone
FROM tbl_1, tbl_2
WHERE tbl_1.id = tbl_2.id
AND tbl_1.id = 2
Or using join:
SELECT id, firstname, lastname, email, phone
FROM tbl_1
INNER JOIN tbl_2
ON tbl_1.id = tbl_2.id
WHERE tbl_1.id = 2;
I have two tables:
a customer table with attributes: firstName, lastName, address, email, password.
a creditcards table with attributes: id, firstName, lastName.
The goal is I need to insert a customer into the customer table only if he/she exists in the creditcards table. All the information are from user's input.
This is what I try to do:
IF EXISTS
(SELECT first_name, last_name
FROM creditcards
WHERE creditcards.first_name = ?
AND creditcards.last_name = ?
)
BEGIN
INSERT INTO customers(first_name, last_name, address,email,password)VALUES (?,?,?,?,?)
END
I also did the setString for the seven "?". But it complains there is syntax error in this statement.
You cannot use IF control block outside of functions in MySQL. That is the reason behind your syntax error. A way to construct your query would be a SELECT inside an INSERT, something like the following:
INSERT INTO customers (`first_name`,`last_name`,`address`,`email`,`password`)
SELECT first_name, last_name, address, email, password
FROM (SELECT ? as first_name, ? as last_name, ? as address, ? as email, ? as password) a
WHERE EXISTS (SELECT 1 FROM creditcards b WHERE a.first_name = b.first_name AND a.last_name=b.last_name);
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
I have a table of customer orders with columns email, firstname and surname. I want to email everyone who has purchased. I thought that using:
SELECT DISTINCT email, firstname, surname FROM ...
would work to return all unique email addresses and first name and surname.
SELECT
contact_email, firstname, surname
FROM
customer_details
GROUP BY contact_email
Haha, used GROUP BY instead :)
I need to find users that have been inserted twice in a table, but with their first name & last name swapped.
e.g. Bob Smith is in the database as
firstname:Bob
lastname:Smith
&
firstname:Smith
lastname:Bob
What's the best query to find those users?
The server runs mysql.
Select
*
FROM UserTable ut
JOIN UserTable ut2 on ut2.firstname = ut.lastname and ut2.lastname = ut.firstname
SELECT
firstname, lastname
FROM
(
SELECT firstname, lastname FROM MyTable -- WHERE firstname <> lastname
UNION ALL
SELECT lastname, firstname FROM MyTable -- WHERE firstname <> lastname
) foo
GROUP BY
firstname, lastname
HAVING
COUNT(*) > 1
SELECT orig.firstname, orig.lastname
FROM yourtable AS orig
INNER JOIN yourtable AS dupes ON orig.firstname = dupe.lastname AND orig.lastname = dupe.firstname
Basically, do a self-join on the user table, but only on the records where the fn/ln dupe-swap occurs.