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)
Related
I hope you are well :).
I'm a little bit stuck with a problem in sql that I can't solve. I would like to get all the people who are not yet friends with a user without tuple.
That is to say that in my column user_id or contact_user_id, it would be necessary that if: user_id = 1 and contact_user_id = 2 (they are friends) that it does not show for the user 2 that it is not friends with user 1.
It should display for user 2 that he is missing user 3,4 and not 1 nor himself
I have for the moment a request of the kind but it does not display the good expected result.
Thanks a lot to the person who will take the time to answer
Their is the desired result without the first line (cause their are already friend) :
I got for the moment this sql statement :
select us.user_id, us.login
from user us where u.user_id!=2
and not exists (select 1 from contact ctc
where ctc.contact_user_id = us.user_id
and ctc.user_id = 2);
EDIT : I maybe founded the sql request
select u.*
from user u
where not exists (select 1
from contact c
where (c.user_id = u.user_id and c.contact_user_id = 2) or (c.contact_user_id= u.user_id and c.user_id=2)
);
If you want records where there is no reciprocal relationship (as your description implies), you can use not exists:
select c.*
from contact c
where not exists (select 1
from contact c2
where c2.contact_user_id = c.user_id and
c.user_id = c.contact_user_id
);
If you want additional information from another table, just join in the outer query:
select c.*
from contact c join
user u
on c.user_id = u.user_id
where not exists (select 1
from contact c2
where c2.contact_user_id = c.user_id and
c.user_id = c.contact_user_id
);
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;
I am having a hard time understanding joins on mySQL, and I cannot find any similar example to work with.
Suppose I have two tables: users and users_info.
in users I have id, email and password fields while, in users_info I have all their information, like name, surname, street, etc.
so, if I am getting a user like this:
SELECT * FROM users WHERE id = 43
and their information like this:
SELECT * FROM users_info WHERE id = 43
I will basically get 2 results, and 2 tables.
I understand now that I need to use join so that they are all together, but I just can't figure out out.
Any help?
It seems like both tables users and user_info are related with each others by the column id therefore you need to join them using this column like this:
SELECT
u.id,
u.email,
u.password,
i.name,
i.surname,
i.street
FROM users AS u
INNER JOIN user_info AS i ON u.id = i.id;
This will only select the fields id, email, ... etc. However, if you want to select all the columns from both the tables use SELECT *:
SELECT *
FROM users AS u
INNER JOIN user_info AS i ON u.id = i.id;
If you want to input the id and get all of these data for a specific user, add a WHERE clause at the end of the query:
SELECT *
FROM users AS u
INNER JOIN user_info AS i ON u.id = i.id
WHERE u.id = 43;
For more information about JOIN kindly see the following:
Join (SQL)From Wikipedia.
Visual Representation of SQL Joins.
Another Visual Explanation of SQL Joins.
Here's an example
SELECT * FROM users u
INNER JOIN users_info i
ON u.id=i.id
this means, you are joining users table and users_info table
for example
users
id name
---- -------
1 abc
2 xyz
users_info
id email
--- ------
1 abc#aaa.com
2 xyz#aaa.com
the query will return
id name email
--- ----- -------
1 abc abc#aaa.com
2 xyz xyz#aaa.com
Here's a nice tutorial
You can also do:
SELECT users.*, users_info.*
FROM users, users_info
WHERE users.id = users_info.id AND users.id = 43;
This means:
"Get me all the columns from the users table, all the columns from the users_info table for the lines where the id column of users and the id column of users_info correspond to each other"
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
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