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;
Related
I need to find duplicate uses based on either same email OR first_name, last_name combination OR same birth_date. What I could comfortably try was:
SELECT id, first_name, last_name
FROM users
where id IN (SELECT id
from users
GROUP BY email
HAVING count(*) > 1)
GROUP BY email, id;
The above gives only duplicate email details, but I'm bit confused about handling other conditions based on first_name, last_name combination OR same birth_date as well.
Is it possible to achieve it in a single query?
Try doing a UNION of three separate queries which checks for the three duplicate criteria:
SELECT id
FROM users
GROUP BY id
HAVING COUNT(DISTINCT email) > 1
UNION
(
SELECT id
FROM users t1
INNER JOIN
(
SELECT firstname, lastname
FROM users
GROUP BY firstname, lastname
HAVING COUNT(*) > 1
) t2
ON t1.firstname = t2.firstname AND
t1.lastname = t2.lastname
)
UNION
SELECT id
FROM users
GROUP BY id
HAVING COUNT(DISTINCT birthdate) > 1
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);
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 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.
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;