NULL value on LEFT JOIN operation - mysql

Consider two tables:
• draft_procedures
• incoterms
Table draft_procedures has column
1. incoterm
Table incoterm has column
1. incotermUNIQUE
2. incoterm_desc
Im trying to join up the two tables in order to get incoterm_desc (description) from incoterm table of draft_procedures incoterm.
When I use the following query I get a c3incoterm_desc NULL value
SELECT drp.incoterm, c3.incoterm_desc as c3incoterm_desc
FROM draft_procedures AS drp
LEFT JOIN incoterms AS c3 ON drp.incoterm = c3.incotermUNIQUE

LEFT OUTER JOIN:
As per the Definition,LEFT OUTER JOIN retrieves rows from TableA (draft_procedures) with matching records from TableB (incoterms)
If for a certain record from TableA (left), there are no matching records from TableB (right), the corresponding (right) columns contain nulls.
Select *
FROM TableA
LEFT OUTER JOIN TableB
on TableA.name = TableB.name;

Related

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

Returning two columns from different tables by joining on one column

I have 200+ telephone numbers in Table A which I require address data for from Table B. Table B has 5 million+ rows of data and the matching field is PhoneNumber. I have written some SQL which does part of what I want but it is only matching up the first record in Table A and I have null values for all of the others???
Please help
SELECT TableA.TelephoneNumber, TableB.Address
FROM TableA LEFT OUTER JOIN
TableB
ON TelephoneNumber = PhoneNumber
If you only want matching records, you should use an inner join, not an outer join:
SELECT TableA.TelephoneNumber, TableB.Address
FROM TableA
JOIN TableB ON TelephoneNumber = PhoneNumber

Condition on Left Join

I am trying the below queries where TABLE B is empty AND records in TABLEA
--This query fetched no records
SELECT TABLEA.COLA,TABLEA.COLB FROM TABLEA
LEFT JOIN TABLEB
ON TABLEA.ID=TABLEB.ID
WHERE TABLEB.COL1<>'XYZ'
--This query fetched records .
SELECT COL1 FROM
(
SELECT TABLEA.COLA,TABLEA.COLB FROM TABLEA
LEFT JOIN TABLEB
ON TABLEA.ID=TABLEB.ID
)A WHERE COL1 <>'XYZ'
Could you help me why first query didnt return any records though they look same. My understanding of first query is "I did a left join so if records doesnt exist in tableb, it should be replaced with NULL values. As NULL <>'xyz' all records should be fetched right..
Placing a WHERE condition on the OUTER joined table of an OUTER JOIN effectively renders that join as an INNER JOIN. So, if there are no rows in the outer table which satisfy the condition, then no rows will be returned.
The solution then is to include any such conditions within the join itself. In the example above this is as simple as changing WHERE to AND.
The one condition that must be placed in the WHERE clause is the test for NULL, the so-called exclusion join - I.e. when you actually want to return the inverse set.

Left Join in Mysql?

This is my first Table.
Then the second one is
Now I am trying to do Left join like this
SELECT t1.StackID FROM t1 LEFT JOIN t2 on t1.StackID=t2.StackID
Output
I am confused here, Is this the correct output? Is not this supposed to return only the 5 rows which is present in left side table.
It's correct output. You are doing LEFT JOIN, so for every record in LEFT table DBMS will 'concatenate' the corresponding RIGHT table record(-s) (and NULL, if there's no corresponding record for the JOIN condition; also remember, that if there are more that 1 corresponding record - all will be joined - this issue is why you're getting not only 5 records from 1-st table).
The thing you're trying to achieve should be done by either DISTINCT modifier, i.e.
SELECT DISTINCT t1.StackID FROM t1 LEFT JOIN t2 ON t1.StackID=t2.StackID;
More about JOIN in SQL you can read here.
There is a simple example of left join:-
SELECT * FROM a JOIN b LEFT JOIN c ON (c.key=a.key) LEFT JOIN d ON (d.key=a.key)
WHERE b.key=d.key;
These extra repeated values of StackId are coming from the join, just use DISTINCT to get those only 5 values:
SELECT DISTINCT t1.StackID
FROM t1
LEFT JOIN t2 on t1.StackID=t2.StackID;
SQL Fiddle Demo
Yes, this is the expected output.
To get the distinct IDs, use DISTINCT -
SELECT DISTINCT t1.StackID
FROM t1
LEFT JOIN t2
ON t1.StackID=t2.StackID
When you left joined your t2 table with t1 based on stackID, then the database will join every row of t2 with the every row of t1 which has the same stackID value. Since 1 appears in six rows in the t2 table as a stackID value, all of them are joined with the first row of t1. Similar thing is true for all other values.
To give yourself a little more understanding of how a left join works try this:
SELECT t1.StackID, t2.StackID FROM t1 LEFT JOIN t2 on t1.StackID=t2.StackID
basically a left join returns all the rows from the left table plus matching ones from the right.