I have two tables which have member data (linked with 'member_id' column)
I need to search for all records where the email column ends in '.pl'. I then need to delete all records from the two tables for this (based on the 'member_id').
Is it possible to complete this in one SQL statement?
SELECT member_id
FROM exp_members
WHERE email LIKE '%.pl'
delete t1, t2
from exp_members t1
join table2 t2 on t1.member_id = t2.member_id
where t1.email LIKE '%.pl'
Delete from Members where Member_id = (SELECT member_id
FROM exp_members
WHERE email LIKE '%.pl')
Related
I have to write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id. Id is the primary key column for this table.
This is the query I wrote:
delete from Person
where Email not in (select distinct Email from Person)
But I get this error:
You can't specify target table 'Person' for update in FROM clause
You Can Try This .
DELETE p1 FROM Person p1
INNER JOIN Person p2
WHERE
t1.id < t2.id AND
t1.Email = t2.Email ;
You are using MySQL, which does not allow that syntax. Use join instead:
delete p
from Person p left join
(select email, min(id) as min_id
from person p
group by email
) pp
on pp.id = min_id
where pp.min_id is null; --no match
Of course, your logic is wrong anyway. But even with the correct logic, not in/not exists is not going to work in MySQL. You need to use a join of some sort.
you can use IN keyword after grouping the Person table to Email
delete from Person where id not in (
select min(id) as ID from Person group by Email)
I need help with a problem I have about SQL.
I have three tables, Table1, Table2, Table3 all with an "email" field with which they relate to each other.
On the other hand, I have an array with a list of emails that I have to check if they are in one of those tables. These emails also have them saved in a string separated with commas and with their respective quotes to use it in an IN (....):
$emails = ("'email1#example.com', 'email2#example.com', ....");
SELECT *
FROM Table t1
INNER JOIN Table2 t2 ON t1.email = t2.email
INNER JOIN Table3 t3 ON t1.email = t3.email
WHERE (t1.email IN ($ emails)) OR (t2.email IN ($ emails) ) OR (t3.email IN ($ emails));
With the previous example, I realized that when doing an INNER JOIN I was matching the tables by the email field which did not have the value in all the tables. It may be the case that an email is not in some table but in another yes, so I need an SQL query so that I get the emails that are in ANY of the 3 tables.
I have been told that I made a temporary table of the emails array and then doing LEFT JOIN between the tables I could get it, but I do not know how to do it.
Could you give me an idea of how to solve this problem?
Thanks in advance.
You need to use UNION, not JOIN.
SELECT email, "Table1" AS whichTable
FROM Table1
WHERE email in ($email)
UNION
SELECT email, "Table2" AS whichTable
FROM Table2
WHERE email in ($email)
UNION
SELECT email, "Table3" AS whichTable
FROM Table3
WHERE email in ($email)
Using SELECT * in all the queries assumes that all the tables have similar columns in the same order. If not, you'll need to list the columns explicitly in each query.
Here is a straightforward way that should return a single Boolean value. EXISTS is useful because it (should) ignore looking at any additional rows as soon as it finds one.
SELECT
EXISTS (SELECT * FROM table1 WHERE email IN (...))
OR
EXISTS (SELECT * FROM table2 WHERE email IN (...))
OR
EXISTS (SELECT * FROM table3 WHERE email IN (...))
I have two tables with users and I want to select the users from the first table which do not exist in the second. Can you help me?
When I use the code
Select t1.user_name From t1 Inner Join t2 On t1.user_name != t2.user_name;
I get all the users many times (actually as the number of the users - 1).
Use a LEFT JOIN instead like
Select t1.user_name From t1 left join t2
On t1.user_name = t2.user_name
where t2.user_name is null;
You can use EXISTS like this
SELECT t1.user_name FROM t1 WHERE NOT EXISTS (SELECT 1 FROM t2 WHERE t1.id = t2.id)
This example assumes that you have some sort of ID on the tables that represents the primary key and foreign key.
Not sure how are your tables designed, but having the same info (user_name) in more than one table is considered as duplication of data. To fix this, you should read about Database normalization
I have three tables that I need to query in order to get my results.
Table 1 has an AppId and a ProjectID
Table2 has an AppID and an AppName.
Table 3 has a ProjectID and a ProjectName
I want to get out of this a list, By AppName, the ProjectNames they are tied to.
So far, a basic query to get what I want works, but I only get the ID's. I need to somehow join these to get the names associated. I need to somehow join this to table2 with the project name information, and table 2 with the appname information.
Select * from Table1 ( this table has only ID's, not names)
Order by AppId
You can join the tables like this:
Select t2.AppName, t3.ProjectName
from table1 t1
inner join table2 t2 on t2.AppID = t1.AppID
inner join table3 t3 on t3.ProjectID = t1.ProjectID
"I have 3 Tables which is Admin table,Studentinfo Table and Useraccount Table.
i want to join that 3 tables for my search engine. :(
im beginner in sql anyone can help me?"
SELECT Username,Name,Position,Status,ImageName FROM useraccount
JOIN admin ON admin.username = useraccount.username
JOIN studentinfo ON studentinfo.username = useraccount.username
where Name Like '%Search%';
"That is my code but nothing happen."
When dealing with multiple tables you would have to compensate for column name ambiguity as the same column name could be available in other joining tables and there is no way for the query execution engine to know which one you want returned.
You can easily fix this by adding an alias or table name to referenced column Name
TableAlias.ColumnName
( or )
TableName.ColumnName
Example:
SELECT Col1
FROM table1
JOIN table2
ON table1.Col1= table2.Col2
Will generate a Ambiguous column name error.
SELECT t1.Col1
FROM table1 t1
JOIN table2 t2
ON t1.Col1= t2.Col2
Or
SELECT table1.Col1
FROM table1
JOIN table2
ON table1.Col1= table2.Col2
The changes:
Added the prefix "t1" or "TableName" to the requested column, so that the engine knows which table we want the data from
Added table aliases in the FROM / JOIN clauses to shorten the code
Scenario Example:
SELECT <TableName>.Username,
<TableName>.Name,
<TableName>.Position,
<TableName>.Status,
<TableName>.ImageName
FROM useraccount
JOIN admin
ON admin.username = useraccount.username
JOIN studentinfo
ON studentinfo.username = useraccount.username
WHERE <TableName>.Name LIKE '%Search%';