MySQL SELECT DISTINCT with WHERE and other fields - mysql

I am trying to SELECT users with unique emails and with a certain Keyword also present. I tried using GROUP BY but that didn't seem to work.
How do I get the other fields? For example, here is my code:
$sql = "SELECT Email, FirstName, LastName, Picture FROM MyTable WHERE Keyword = '$Keyword'";
How would I get these 4 fields with the unique email?

DISTINCT will pick unique from all the selected columns:
$sql = "SELECT DISTINCT Email FROM MyTable WHERE Keyword = '$Keyword'";
GROUP BY will pick unique from all the group by columns
If you want FirstName, LastName, Picture these fields as well then you can use this, as `FirstName, LastName, Picture they are not distinct then multiple details of a particular email will be omitted
$sql = "SELECT Email, FirstName, LastName, Picture FROM MyTable
WHERE Keyword = '$Keyword' GROUP BY Email ";

Perhaps group by with min might work for you
SELECT Email, FirstName, LastName, min(url)
FROM MyTable
group by Email, FirstName, LastName

Related

Find duplicates using MySQL considering multiple columns

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

mysql - insert select from two tables with user input

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);

MySQL Distinct function

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 :)

deleting duplicates

From this SO POST Finding Duplicates, how can I delete duplicates.
SELECT firstname, lastname, list.address FROM list
INNER JOIN (SELECT address FROM list
GROUP BY address HAVING count(id) > 1) dup ON list.address = dup.address
just use the DISTINCT keyword:
SELECT DISTINCT firstname FROM list;
if any of the output is a duplicate, mysql will remove them.
for more documentation on DISTINCT go here:
http://www.cyberciti.biz/faq/howto-removing-eliminating-duplicates-from-a-mysql-table/

mysql - Find duplicate users with firstname/lastname swapped

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.