MySql Join Issue When Join Two Tables - mysql

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)

Related

Update a table after inserting records with an Inner Join

I insert into TableA using a Select/Inner Join from TableB and TableC.
Insert into TableA (C,S,M,C100)
SELECT C,S,M,group_concat(CID) FROM TableB
INNER JOIN TableC
ON TableB.CID= TableC.CID and P>=100 group by C,S,M
Now I need to update those records in two ways. One is identical to the first but now I want to update a different field with P<100, in essence:
Insert into TableA (C,S,M,C0)
SELECT C,S,M,group_concat(CID) FROM TableB
INNER JOIN TableC
ON TableB.CID= TableC.CID and P<100 group by C,S,M
Except I don't want new records I want to update where TableA C,S,M match
The second thing I want to do is similar, but involves updating from a different table but in almost an identical manner
Insert into TableA (C,S,M,C100)
SELECT C,S,M,group_concat(CID) FROM TableD
INNER JOIN TableE
ON TableD.CID= TableD.CID and P>=100 group by C,S,M
In other words I could create each pass as separate inserts but would end up with duplicate records of C,S,M.
Is there a way to do the passes after the first insert as Updates OR is there a way to do them each as Inserts and afterwards combine the records where C,S,M are identical?
Use an update with join :
UPDATE TableA
join (select C,S,M,group_concat(CID) as newCol
FROM TableB
where P<100
group by C,S,M) t
ON (tableA.c = t.c and tableA.s = t.s and TableA.M = t.m)
SET <YourColumn> = t.newCol

delete records based on join result

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.

Oracle Select Query based on Column value

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

Populating new DB table from other table data

Hi there basically i have 2 tables with 162 entries and im trying to populate a new table with entries from the other tables to show the difference between a number value
Insert Into popdiff(
popdiff)
select (a.malepop+a.femalepop)-(b.malepop+b.femalepop)
from tablea a, tableb b;
The problem im having is it is returning 26244 results i.e. 162*162 rather than 162 which im expecting, having looked into it a bit the query is finding the value for each entry in tablea- the 162 values in tableb
how can i simply return just the 162 rows?
You didn't specify the relationship between the two tables and that's a cross join which returns what you described. Specify the relationship either in a WHERE condition or a JOIN condition.
WHERE:
SELECT (a.malepop+a.femalepop)-(b.malepop+b.femalepop)
FROM tablea a, tableb b;
WHERE a.id = b.id;
JOIN:
SELECT (a.malepop+a.femalepop)-(b.malepop+b.femalepop)
FROM tablea a
INNER JOIN tableb b ON a.id = b.id;
Of course, you may have different names for the IDs, so change the a.id = b.id consition accordingly.
You have to do an inner join:
select (a.malepop+a.femalepop)-(b.malepop+b.femalepop)
from tableA join tableB using (field)
That will do the job

Inserting distinct entries into the database

I have two tables with exactly the same fields. Table A contains 7160 records and table B 7130 records.Now I want to insert distinct records from table A into table B such that B should not have any duplicate entry in it. How should I go about doing this?
This basically selects records that are in A that are not in B. It would work, but you might have to tweak the field you use to uniquely identify a record. In this example I used field 'ID' but you might have to change that to A.field1 = B.field1 AND A.field2 = B.field2 etc.
INSERT INTO TABLEB
(
SELECT A.*
FROM TABLEA A
LEFT JOIN TABLEB B ON A.ID = B.ID
WHERE B.ID IS NULL
)
You can use a "union" query to combine the results from multiple tables into a single result set. "union" will only return distinct rows from all tables.
See this page for more info:
http://www.tutorialspoint.com/mysql/mysql-union-keyword.htm
insert into tableB (id)
select t1.id from tableA t1
where t1.id not in (select t2.id from tableB t2)