MySql join returns null on table empty - mysql

I have 2 tables: A and B. I want a query which will return results if any of the values are satisfied in either of the 2 tables. I tried using a join, but it returned null when the second table is empty and vice versa.
Table A
emp_no emp_add data
12 go nice
Table B
emp_no emp_add id
12 go 1
Required output
data id
nice 1
Similarly
Table A
emp_no emp_add data
12 go nice
Table B
emp_no emp_add id
Required output
data id
nice
SELECT A.data, B.id
FROM A left join B ON A.emp_no = B.emp_no
WHERE A.data='nice'
AND a.id='1' ;

use Left Join instead on the table that you accept null values

Try this::
Select ifnull(a.data,'') as data, ifnull(b.id,'') as id from tableA a left join tableB b on (a.emp_no=b.emp_no)

As you told that it would happen vice versa, it would better to use Outer Join in your case:
Select tblA.data,tblB.id from tableA tblA FULL OUTER JOIN tableB tblB on tblA.emp_no=tblB.emp_no

select tableA.data, tableB.id from tableA, tableB

Related

MySql Join Issue When Join Two Tables

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)

How to get a query by selecting rows in the TableA with a particular exclusion in TableB

TableA
clientId clientPassword
1 1234
2 1234
3 1234
TableB
clientId clientCode
1 TRN
2 ABC
3 CDE
3 TRN
What would be the query to select TableA.clientPassword with only those clientID which does not have 'TRN' in TableB.clientCode ?
Part of a complex query but simplified to get my question answered.
Not exists works perfectly and is the logically straight forward way to write this but isn't always the most performant option. Using a not in relies on the DB system to figure out it can flatten the query out to avoid running it row by row. With this simple of a query the DB system likely will figure it out but you can write it in a flattened way.
SELECT a.*
FROM TableA a LEFT JOIN TableB b ON a.clientId = b.clientid
AND b.clientcode = 'TRN'
WHERE b.ClientId IS NULL
To explain this a bit the left join will join table B to table A where the ID's match and the clientcode is 'TRN' but will keep all entries in table A and have NULLs for table B when a record doesn't exist with 'TRN' so then the is null check is equivalent to the not exists in the other query but avoids the row by row checking of a correlated sub-query and should be much faster.
This is a basic not exists;
select a.*
from tableA a
where not exists (select 1
from tableB b
where a.clientId = b.clientId and b.clientcode = 'TRN'
);
More here - https://technet.microsoft.com/en-us/library/ms184297(v=sql.105).aspx

How to merge records from two tables into third using MYSQL

I have 3 tables A, B, C
Schema of all 3 tables is same as mentioned below:
1st A table:
cpid ,name, place
2nd B table:
connectorid,dob
3rd C table:
ccpid cconnectorid
Now both tables A and B have many records.
Now some of the records in A and B are with same id.
Now I want to merge the records from A and B into Table C.
Merge logic is as follows
1)If records with cpid = connectorid ,insert into table c.
2)C Table ccpid is the foreignkey for A table cpid and cconnectorid is the foreignkey B table connectorid.
3)Using select query.
You can use select insert with a n inner join
insert into table_c
select a.cpid, b.connectorid, a.place
from table_b as b
inner join table_a as a on a.id = b.id
You can try this solution for your query:
INSERT INTO `C`(`ccpid`, `cconnectorid`, `ccity`)
SELECT ta.`cpid`, ta.`cconnectorid`, tb.`place`
FROM `A` as ta
INNER JOIN `B` tb ON ta.`cpid` = tb.`cconnectorid`
You just need join data from both tables? This is simple JOIN function.
SELECT *
FROM Table_A
INNER JOIN Table_B
ON Table_A.cpid =Table_B.connectorid;
You can insert this select to your Table_C.
Here is INNER JOIN, but I think you should take a look to JOINs, here are examples and you can read more about other JOINs.
INNER JOIN: Returns all rows when there is at least one match in BOTH
tables LEFT JOIN: Return all rows from the left table, and the matched
rows from the right table RIGHT JOIN: Return all rows from the right
table, and the matched rows from the left table FULL JOIN: Return all
rows when there is a match in ONE of the tables
use following query replace with your table names
INSERT INTO CTABLE(ccpid,cconnectorid,ccity)
(SELECT A.cpid ,B.connectorid, A.place FROM
TABLEA A INNER JOIN TABLEB B ON A.cpid = B.connectorid)

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

Select records from one table where a column value exists in another table

I have 2 MySQL tables A and B.
I would like to select only the records from B where a certain value exists in A.
Example:
A has columns: aID, Name
B has columns: bID, aID, Name
I just want the records from B for which aID exists in A.
Many thanks.
You need to do either INNER JOIN - records that exists in both tables, or use LEFT join, to show records that exists in A and matching IDs exists in B
A good reference:
You need to make a join, and if you don't want to retrieve anything from table b, just return values from table a.
This should work
select b.* from b join a on b.aID=a.aID
Below query will also work and will be effective
SELECT * FROM B
WHERE B.aID IN (SELECT DISTINCT aID FROM A)
You just need a simple inner join between tables A and B. Since they are related on the aID column, you can use that to join them together:
SELECT b.*
FROM tableB b
JOIN tableA a ON a.aID = b.aID;
This will only select rows in which the aID value from tableB exists in tableA. If there is no connection, the rows can't be included in the join.
While I recommend using a join, you can also replace it with a subquery, like this:
SELECT *
FROM tableB
WHERE aID NOT IN (SELECT aID FROM tableA)
You can use join like this.
Select b.col1,b.col2... From tableB b inner join table tableA a on b.field = a.field
Have you tried using a LEFT JOIN?
SELECT b.* FROM tableB b LEFT JOIN tableA a ON b.aID = a.aID