MySQL Distinct function - mysql

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

Related

MySQL SELECT DISTINCT with WHERE and other fields

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

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

Number of duplicates of a pair of columns in sql

I have a table with this format
first_name last_name
and I want to show the pairs of "first_name" and "last_name" that appear exactly twice at the database. I've used this
select first_name, last_name, count(*)
from employees
having count(*)=2;
which is wrong and I am not sure what it's doing. It's my first sql approach so please be patient :) *Working with mysql workbench
You need a group by:
select first_name, last_name, count(*)
from employees
group by first_name, last_name
having count(*)=2;
select first_name, last_name, count(*)
from employees
group by first_name, last_name
having count(*)=2;
You have to use group by and then using having clause for this..
Query is:
select first_name, last_name, count() from employees group by first_name, last_name having count()=2;

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;

copying between multiple tables

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;