I am trying to accomplish the following in MySQL:
delete from table_b where table_b.token is not found in table_a.token
Explanation:
Both tables have a column called token.
I want to delete all records on table_b if token in table_b does not exist in the token
column in table_a.
Using a subquery:
delete from table_b where token not in (select token from table_a)
You can use a join
delete b.*
from table_b b
left join table_a a on(b.token = a.token)
where a.token is null
Related
I have some doubt in MySQL joins
I have 2 tables for example TableA,TableB
TableA primary key is Foreign key of Table B
So I'm using inner join to get matched values but TableB have one column for row activate status so all active status is zero means I need to get that record or else I need to skip that record.
My query:.
Select * From TableA a inner join TableB b on a.id=b.aid where b.isActive=0;
The above query was return value if any one value is true
For example any one of is active row true. But I need to check all row is zero if it's all value zero means I need to return that so how I do that..?
Thanks in advance.
Select a.*
From TableA a
where not exists(SELECT 1 FROM TableB b WHERE a.id=b.aid AND b.isActive=1);
You can use your query change the filter as follows:
SELECT * FROM TableA a
JOIN TableB b ON a.id=b.aid
WHERE a.id NOT IN(SELECT aid FROM TableB WHERE isActive=1)
I have two tables in the mysql as follows:
Table_A
Table_B
I need to join Table_B to Table_A and add the values of BalAmountLC to Table_A where the GLAccount equals. I also need to update other GLAccount values to Table_A which is not exist in Table_A.
Anyone please advise me which is the best way to do it. I feel like I need to use RIGHT JOIN. But I am not sure about that.
I am stuck with the following query:
SELECT Table_A.*,Table_B.*,Table_A.BalAmountLC+Table_B.BalAmountLC as sum FROM Table_A RIGHT OUTER JOIN Table_B ON Table_A.GLAccount=Table_B.GLAccount
Thanks.
So basically you need to INSERT all GLAccount in Table_B to Table_A if it does not exit in Table_A. If exists, you want to add BalAmountLC to Table_A
UPDATE Table_A INNER JOIN Table_B ON (Table_A.GLAccount = Table_B.GLAccount)
SET Table_A.BalAmountLC = Table_A.BalAmountLC + Table_B.BalAmountLC;
INSERT INTO Table_A SELECT Table_B.* FROM Table_B LEFT JOIN Table_A
ON (Table_B.GLAccount = Table_A.GLAccount) WHERE
Table_A.GLAccount IS NULL;
I have two tables that are joined by an id.
Table A is where I define the records.
Table B is where I use the definition and add some data.
I am doing some data normalization and I have realized that on table B there are some ID that are no longer defined in table A.
If I run this query:
SELECT B.id_cred, A.id_cre from B LEFT JOIN A ON B.id_cred=A.id_cre
I see those records that are NULL on A.id_cre.
I'd like to DELETE from table B those records where the query returns null on table A?
Something like:
DELETE FROM B WHERE id IN (SELECT B.id from B LEFT JOIN A ON B.id_cred=A.id_cre WHERE a.id IS NULL)
but this query throws an error because table B is target and reference at the same time.
You can't specify target table B for UPDATE in FROM clause
Note that the join query will return 1408 rows so I need to do it in a massive way
Option 1, use NOT EXISTS:
delete from B
where not exists (select 1 from A where A.id_cre = B.id_cred)
Option 2, use a DELETE with JOIN:
delete B
from B
left join A on B.id_cred = A.id_cre
where A.id_cre is null
This should do the trick:
DELETE FROM B
WHERE NOT EXISTS (
SELECT id_cre
FROM A
WHERE B.id_cred=A.id_cre)
Just delete any row from B where the key does not exist in A.
NOTE: "A" and "B" can't be aliases, they must be the actual table names.
Hope this helps!
Why not use id_cre from A table since both have the id.
This statement:
DELETE FROM B WHERE id IN (SELECT B.id from B LEFT JOIN A ON B.id_cred=A.id_cre WHERE a.id IS NULL)
simply says that both a and b (B.id_cred=A.id_cre) are the same.
I have two tables lets say
Table A
columns id , name address
Table B
columns id , age, import_date
The Table B id is a reference key of Table A.
Now I want to return results from A & B but if the record is not in B I still want to see the record so for this I use left outer join
Select * from A a left join B b
on a.id = b.id
Now even I don't have record in B I still get the record.
Table B may contain duplicate ids but unique import_date.
Now I want to results in a way that if there is duplicate id in table B then I want to get the records only where import_date is as of today.
I still want to get the records for ids which are not there but if the ID is there in table B then I want to apply above condition.
I hope someone can help me with this.
Sample data
Table A
01|John|London
02|Matt|Glasgow
03|Rodger|Paris
Table B
02|22|31-AUG-2015
02|21|30-AUG-2015
02|23|29-AUG-2015
The query will return
01|John|London|null|null|null
02|Matt|Glasgow|22|31-Aug-2015
03|Rodger|Paris|null|null
You almost got the solution. Just add one more condition like below
Select a.id,a.name,a.address,b.age,b.import_date
from tablea a left join tableb b
on a.id=b.id and b.import_date=trunc(sysdate)
order by a.id;---This line optional
Check the DEMO HERE
SELECT *
FROM Table_A t1 LEFT OUTER JOIN Table_B t2 ON t1.id=t2.id UNION
SELECT *
FROM Table_A t1 LEFT OUTER JOIN Table_B t2 ON t1.id=t2.id
GROUP BY t2.import_date
HAVING t2.import_date=CURDATE();
I have two similar tables table_a and table_b
table_a is my current data and I need to update this info using table_b which is a temporary table. The only difference between the two is table_a has a password field that table_b will not have.
I am trying to do a couple of things.
1.Compare the data based on the "user_id" field.
2. If there is a user_id not found in table_b but there is one in table_a remove that row in table_a
3. If there is a user_id in table_b that is not found in table_a add that row of data to table_a
4. If the user_id is found in both then check two of the fields "email" and "job_code" and make sure they are both whatever table_b says.
Should I do this in separate MySQL statements in the order I have numbered above? below is my try at the above statements. Any help or troubleshoot is greatly appreciated. Thanks!
Statement 1:
SELECT * FROM table_a
WHERE table_a.user_id
NOT IN (
SELECT table_b.user_id
FROM table_b
WHERE table_a.user_id=table_b.user_id
) // But how do I delete those records that are selected?
Statement 2:
SELECT * FROM table_b
WHERE table_b.user_id
NOT IN (
SELECT table_a.user_id
FROM table_a
WHERE table_a.user_id=table_b.user_id
) //How do I Insert these records that I have in table_b but not in table_a
Statement 3:
SELECT email FROM table_b
WHERE table_b.user_id = table_a.user_id,
AND table_b.email != table_a.email //now I need to update table_a with the emails from table_b
Statement #1
DELETE A.*
FROM table_a A
LEFT JOIN table_b B
USING (user_id)
WHERE B.user_id IS NULL;
Statement #2
INSERT INTO table_b (column1,column2,...,columnN)
SELECT A.column1,A.column2,...,A.columnN
FROM table_b B LEFT JOIN table_a A
USING (user_id)
WHERE A.user_id IS NULL;
Statement #3
UPDATE table_a A INNER JOIN table_b B USING (user_id)
SET A.email = B.email,A.job_code = B.job_code;
UPDATE 2012-06-19 11:54 EDT
A and B are just aliases for the table.
For example, the query of Statement #3 could have been written like the following:
UPDATE table_a as A INNER JOIN table_b as B USING (user_id)
SET A.email = B.email,A.job_code = B.job_code;
or with no aliases whatsoever, like this:
UPDATE table_a INNER JOIN table_b USING (user_id)
SET table_a.email = table_b.email,table_a.job_code = table_b.job_code;
The solution of the first problem ::
1)
delete FROM table_a
left join table_b
on
table_a.user_id=table_b.user_id
and table_a.user_id is null