I want to contact my users via email using my database. I want to make sure that I don't accidentally contact the same user twice. For that, I have a table that tracks who got contacted and when.
When I do my MYSQL query I want to select emails from the the email table and make sure none of those entries exists in the contacted table.
To phrase it in a sentence: select email from Email_Table if they are not in Contacted_Table
Perhaps there is a completely different approach. I am open to all suggestions :)Thank you :)
Try this
SELECT email FROM email_table e
LEFT JOIN contacted_table c ON e.email = c.email
WHERE c.email IS NULL
select email
from Email_Table t1
where not exists (select email
from Contacted_table t2
where t1.email = t2.email)
OR
select email
from Email_Table t1
where email not in (select email
from Contacted_table)
If you try to do a left join like this:
SELECT users.email, contacted.email
FROM users LEFT JOIN contacted ON users.email = contacted.email
You will get a similar result:
users.email | contacted.email
-----------------------------
aa#aa.com | aa#aa.com
bb#bb.com | bb#bb.com
cc#cc.com | cc#cc.com
dd#dd.com | NULL
ee#ee.com | NULL
Your goal is to get those record which do not have a match in the contacted table, so your query would be:
SELECT users.email
FROM users LEFT JOIN contacted ON users.email = contacted.email
WHERE contacted.email IS NULL
Related
I have a database with 2 tables like this (much larger than this)
users
Name | Email
Bill billy#gmail.com
Susan susan#gmail.com
-
bad_emails
Email
billy#gmail.com
bademail#gmail.com
I want to delete anything from users where the email is in the bad_emails table.
I tried something like this
DELETE FROM users WHERE (SELECT Email FROM bad_emails) as bad_emails = users.Email
But that's a syntax error, not sure on the correct syntax.
Multiple ways.
JOIN:
DELETE u
FROM users AS u
JOIN bad_emails AS b ON u.email = b.email
WHERE IN:
DELETE FROM users
WHERE email in (SELECT email FROM bad_emails)
WHERE EXISTS:
DELETE FROM users AS u
WHERE EXISTS (SELECT 1 FROM bad_emails AS b WHERE b.email = u.email)
I have the following MySQL query.
SELECT
login,
firstname,
lastname,
email
FROM
xcart_customers
WHERE
login LIKE 'anonymous%'
AND email NOT IN (
SELECT
email
FROM
xcart_customers AS cx
WHERE
cx.login NOT LIKE 'anonymous%'
)
GROUP BY
email;
Basically there are two sets of customers, customers that have logins, and anonymous customers who all start with a login of 'anonymous%'.
I am trying to remove non-anonymous users from the list, that have the same email address as the anonymous users.
I thought the above query would have worked, but I still get some emails addresses that match non-anonymous users.
login | firstname | lastname | email
---------------------------------------------------------------------------
anonymous-10 | Eric | Byorn | byorn#mail.com
---------------------------------------------------------------------------
some_user_name | Eric | Byorn | byorn#mail.com
---------------------------------------------------------------------------
So I am trying to solve, all anonymous users, who only appear in the anonymous results.
The most efficient, and IMHO elegant, solution is to use an outer join:
SELECT
a.login,
a.firstname,
a.lastname,
a.email
FROM xcart_customers a
LEFT JOIN xcart_customers b ON b.email = a.email
AND b.login NOT LIKE 'anonymous%'
WHERE a.login LIKE 'anonymous%'
AND b.email IS NULL
There's two key tricks in play here:
Because missed LEFT JOINs return all NULLs in the left joined table, the WHERE clause excludes rows that have a match
All conditions (even non-key) on the joined table must be in the join condition, ie not in the WHERE clause, otherwise to efficively turn the outer join into an inner one
Note that your use of GROUP BY is both flawed and unnecessary.
I have a table for users like this
id | name | password | email
1 saeid ***** asd#asd.com
I have another table called appointments
id | created_by | due_date | notification_send
1 1 ***** 0
I want to get all users from users table where they have at least created one appointment in the appointments table (denoted by created_by field in the appointments table).
I have tried the code below but it fails:
SELECT * FROM users LEFT JOIN appointments a ON persons.id = a.created_by
But obviously it does not work.
One way is to use the exists predicate:
SELECT * FROM users u
WHERE EXISTS (SELECT 1 FROM appointments a WHERE a.created_by = u.id)
Alternatively you could use an inner join, but the exists query corresponds better to your question in my opinion (that is if you only need data from the users table).
The left join says to get all rows from users regardless if they have matching rows in appointments which is not what you want.
You are searching for a match between the table and so I would suggest doing a INNER JOIN rather like below
SELECT * FROM users u
JOIN appointments a ON u.id = a.created_by
Also check your ON clause once I think either this is a typo or a big mistake. You are selecting from users table then why persons.id??
ON persons.id = a.created_by
Try something like this:
http://sqlfiddle.com/#!9/5eba3/2
select * from users c where (select count(*) from appointments where created_by = c.id) > 0;
A contact has many emails. What I would like is this:
contacts.id | contacts.name | emails.address | "emails.address 2"
1 Sheldon Cooper sheldon#gma.com shelly#coop.com
What I get is:
contacts.id | contacts.name | emails.address
1 Sheldon Cooper sheldon#gma.com
1 Sheldon Cooper shelly#coop.com
The query I use is:
SELECT *
FROM contacts
LEFT JOIN emails
ON contacts.id=emails.contactsId
One to many relationship and generating them as extra columns in the query result. I think I need to use group concat but can't seem to get my head round it.
SELECT a.id, a.name,
GROUP_CONCAT(b.address ORDER BY b.id) AS "email addresses"
FROM contacts a
LEFT JOIN emails b ON a.id = b.contactsId
GROUP BY a.id,a.name
The above gives in one column all the addresses separated by commas.
SELECT *, (select group_concat(address)
from emails
where contacts.id=emails.contactsId)
FROM contacts
You need to use group_concat
select c.id, c.name, group_concat(g.address)
from contacts c inner join emails g
on c.id=g.contactsId
group by c.id
I have a users table in MySQL.
id | username | email
I would like to get the IDs of duplicated users based on the email address. So basically find the Ids of users where the email address can be found more than 1 times in the table.
I wrote something like this:
SELECT id
FROM users as users
WHERE (
SELECT count(id)
FROM users as users2
WHERE users.email = users2.email
) > 1
It works ok, but very slow, as the query doesn't seem to use the index. Any other ideas? Thanks!
The solution :
select u.id
from users as u
inner join
( SELECT email
FROM users
GROUP BY users.email
HAVING count(id) > 1
) as u2 on u2.email = u.email