mysql - insert select from two tables with user input - mysql

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

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 return result if value is empty in another table

I have 2 mysql tables "users1" and "users2". They both have the following fields
users1: email, zipcode, first_name, last_name, processed
users2: email, zipcode, first_name, last_name
I have the following SQL query:
SELECT zipcode FROM users1 WHERE processed IS NOT NULL
Currently this query returns the result from users1 table. However, Before returning the result, I would like to check that "zipcode" DOES NOT EXIST in the users2 table.
So basically, it needs to only return the result if the returned zipcode from "users1" does not exist in "users2"
Any idea how I can do this?
Thanks
You can use NOT IN:
SELECT zipcode
FROM users1
WHERE processed IS NOT NULL
AND zipcode NOT IN
(SELECT distinct zipcode from users2)
Use an outer join filtering to get only rows not present in the other table:
SELECT u1.zipcode
FROM users1 u1
LEFT JOIN users2 u2 ON u2.zipcode = u1.zipcode
WHERE processed IS NOT NULL
AND u2.zipcode IS NULL
Try this untested query:
select user2.zipcode from user2 where user2.zipcode not in (SELECT zipcode FROM users1 WHERE processed IS NOT NULL)

Inserting data from one table to other but don't want the primary id

I am trying to copy data from one table to another table but I don't want to have the ID column copied into the the new table.
Here is what I have tried so far
INSERT INTO T2 (ID, FIRST_NAME, LAST_NAME)
(SELECT AUTO, FIRST_NAME, LAST_NAME,
FROM T1);
When the table T2 was created the ID was not set to auto-increment since the IDs are going to be generated by the front end app going forward.
Don't know if this has been asked before but please help.
INSERT INTO T2 (ID, FIRST_NAME, LAST_NAME)
(
SELECT (#row:=#row+1) AS rn,FIRST_NAME, LAST_NAME,
FROM T1, (SELECT #row:=0) r
);
check the query at SQL Fiddle: http://www.sqlfiddle.com/#!2/91037/1

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

INSERT SELECT query when one column is unique

I need to call INSERT + SELECT to import user data to different table and I need to filter user_name duplications, So I need something like this:
INSERT INTO new_table SELECT email, distinct user_name, password from old_table
but distinct works only when using distinct email, user_name, password and all those column need to be unique.
Is there any other way to insert select with uniq user_names, (I need only first row - with lower id)?
EDIT I forget to mention that I use mysql
Without testing I would expect something like this to do the trick (assuming the id column is named id)
INSERT INTO new_table
SELECT email, user_name, password
FROM old_table
INNER JOIN
( SELECT MIN(id) FROM old_table GROUP by user_name ) minids
ON minids.id = old_table.id