mysql command to get data of 2 tables - mysql

I would like to get data from 2 mysql tables.
Table tbl_1:
Table tbl_2:
I tried this:
SELECT `tbl_2`.*
FROM `tbl_1`
LEFT JOIN `tbl_2` ON `tbl_1`.invoiceID = `tbl_2`.invoiceID
WHERE `tbl_1`.customerID = "463";
My result:
The results looks good but not perfect.
I get many null rows which are not be in my result.
only the first both rows should be in the result.
Where is my fault?

The LEFT JOIN keyword returns all records from the left table (tbl_1), and the matching records from the right table (tbl_2).
Maybe you should try 'INNER JOIN' instead of 'LEFT JOIN'?

The LEFT JOIN keyword returns all records from the left table tbl_1, and the matching records from the right table tbl_2. So change the order of the tables
SELECT `tbl_2`.*
FROM `tbl_2`
LEFT JOIN `tbl_1` ON `tbl_1`.invoiceID = `tbl_2`.invoiceID
WHERE `tbl_1`.customerID = '463';

Sir,
You have two tables (Table1 and Table2) and there is a COMMON column called invoiceID in both your tables. So if you want to select some columns from these two tables for a given CUSTOMERID:
SELECT Table1.*, Table2.*
FROM Table1 INNER JOIN Table2
ON Table1.invoiceID = Table2.invoiceID
WHERE Table1.CUSTOMERID = "463"

Related

Issue SQL join query

I am getting issues with the below SQL query, unable to fetch the desired result
SELECT
C.Department__c
,C.Email
,R.AcctID__c
,C.ContactID__c
,R.TransactionDueDate
,R.PubNbr__c
FROM RenewalNotificationProgramDE R
LEFT JOIN ContactNewDE C
ON R.AcctID__c = C.AcctID__c
The idea is to join all the AccountID(AcctID__c) in RenewalNotificationProgramDE table to the corresponding contacts in ContactNewDE table. AccountID is the foreign key in the ContactNewDE table. I am usig Innerjoin in my query as I want all AccountID to map with their corresponding contacts in ContactNewDE.
Just replace LEFT JOIN by INNER JOIN to get all AccountID with their corresponding contacts :-
Use below query :-
SELECT
C.Department__c
,C.Email
,R.AcctID__c
,C.ContactID__c
,R.TransactionDueDate
,R.PubNbr__c
FROM RenewalNotificationProgramDE R
INNER JOIN ContactNewDE C
ON R.AcctID__c = C.AcctID__c
You have used LEFT JOIN in your query, which you need to change it to INNER JOIN. I think you would better to notice the below notes about differences between left and inner 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

MySQL Joins not working as required

I have two tables.
Table A:
root_id, root_text
Table B:
word_id , word_text,root_text
Now Some of the records of Table B has root_texts which might not exist in Table A.
So for that I am trying to use left join.
select *
from A a , B b
where a.root_text=+b.root_text
I only get the records which has matching root_text.
If I use the new way
Select
*
from
A left join B on a.root_text= b.root_text
I get so many extra records. I am Using MySQL 5.6.1.7
UPDATE :
http://sqlfiddle.com/#!9/7b32a/2
Query I am running
select * from word_detail w left join roots r
on r.root_text =w.root
and w.surah_no=1
and w.verse_no=1
and w.word_no=1
What I am doing wrong ?
As in result you see so many records which are not needed. The filter verse_no , word_no not working.
Update
The issue was after the left join we have to use where
select * from word_detail w left join roots r
on r.root_text =w.root
where w.surah_no=1
and w.verse_no=1
and w.word_no=1
If you want all records from TableB that are not present in table A you should use this:
Select *
from
B left join A
on a.root_text= b.root_text
where
a.root_text is null
or if you want the opposite - all records from tableA that are not present on tableB:
Select *
from
A left join B
on a.root_text= b.root_text
where
b.root_text is null
btw, this is not a left join on MySQL:
select * from A a , B b where a.root_text=+b.root_text
but will result as a simple INNER JOIN
The += operator is not standard SQL, but specific to the Oracle RDBMS, so it will not work as expected on MySQL.
The LEFT JOIN syntax is correct, the "many extra records" stem from the data in your tables. You might want to use some sort of WHERE as a filter or an aggregate to group the result set to make it more managable.
Your first example is an inner join which explains why you are not getting as many results as when you are left joining. Left joining can also be thought of as a left outer join.

Left join issue with reference table

Im having a bit of difficulty with getting user information from one place to another.
There are 3 tables dbo.gr_usersource and dbo.gr_task and dbo.gr_user
In the dbo.gr_task table a column is filled with values that match entries in dbo.gr_usersource table that has another value that corresponds to the value in the dbo.gr_user table. You could call it a reference table between dbo.gr_task and dbo.gr_user tables.
My query looks like this;
select
dbo.gr_task.task_number
, dbo.gr_task.task_name
, dbo.gr_task.task_description
from dbo.gr_task
left join dbo.gr_user AS Handler
on dbo.gr_usersource.usersource_user = Handler.user_id
and dbo.gr_task.task_handler = dbo.gr.usersource.usersource.id
The last step would be to get the column user_name from table user when the join is working.
You have missed mediator table in your join so use as per below-
SELECT dbo.gr_task.task_number,dbo.gr_task.task_name, dbo.gr_task.task_description
FROM dbo.gr_task AS gt
LEFT JOIN dbo.gr_usersource gus ON gt.task_handler=gus.usersource.id
LEFT JOIN dbo.gr_user AS gu ON gus.usersource_user=gu.user_id;
Note: If you want only matching rows in all 3 tables then you should use normal join instead of left join.
This may work for you.
Note that there are no columns from your joined tables in your select list so left joins would have no impact on your result set.
Inner joins will filter your results set even if you bring back no columns, i.e., enforce the join condition to match rows in both tables.
SELECT
t.task_number
, t.task_name
, t.task_description
FROM dbo.gr_task t
INNER JOIN dbo.gr_usersource us
ON us.usersource.id = t.task_handler
INNER JOIN dbo.gr_user u
ON u.user_id = us.usersource_user

Mysql Select Query based on value from different table

This is a very basic question, I apologize if someone has already asked this question..
I have a main table called 'item'. It has all the item(s) related information such as catalog,description,avail,price etc
I also have another table called 'activelisting' which only have one column 'catalog'.
Now I want to pull 'catalog','description', and 'avail' from 'item' table based on 'catalog' exists in 'activelisting' table.
or another words everything from 'activelisting' table and matching records from 'item' table.
I have researched it a lot but I am unable to find working query. I am using the following query..
SELECT t1.catalog, t1.description, t1.avail
FROM item t1
LEFT JOIN activelisting t2
ON t1.`Catalog` = t2.`Catalog`;
any help would be appreciated...
Try using INNER JOIN instead of LEFT JOIN. INNER JOIN will guarantee that the value being joined on exists in both tables.
SELECT i.catalog, i.description, i.avail
FROM item i
INNER JOIN activelisting a ON i.catalog = a.catalog
LEFT JOIN will return all results in the left table, item and if there is not a row in activelisting with the same catalog then it will just return NULL for those columns. You are not currently selecting the potential NULL columns, so your current result should basically just be all of the entries in item.
SELECT t1.catalog, t1.description, t1.avail FROM item t1 RIGHT OUTER JOIN activelisting t2 ON t1.Catalog = t2.Catalog;
OR
SELECT t1.catalog, t1.description, t1.avail FROM activelisting t2 LEFT OUTER JOIN item t1 ON t1.Catalog = t2.Catalog;
The first one for good visualizer. You have to use outer join just because you take all catalogs from activlisting table and matched catalog from item table.
Second one for better understanding.

Join two table with everything

I have to tables that I want to get all the results of both connect by a common cell. The problems is the second table only has some of records of the first table.
Table 1 forms
form_id, description, image,dept
Table 2 records
record_id, form_id, comments, added_date, done_date
If do a query like this:
SELECT * FROM form
JOIN records ON record.form_id = form.form_id
I do not get all of the forms because there is no record for that form. Is there away to do something like this? I would create a blank record for each form on in the records table, but I could not figure that out either.
This is a perfect application for a LEFT OUTER JOIN.
Example:
SELECT f.*, r.*
FROM form f
LEFT JOIN records r
ON r.form_id = f.form_id
ORDER BY f.description, r.added_date;
Use left join for these cases
SELECT * FROM form
LEFT JOIN records ON record.form_id = form.form_id
Please check which one u need --
LEFT OUTER JOIN :
Will include all records from Table Mentioned in Left Side and Matched Records from Right Side table, unmached record will be null
Ex :
SELECT *
FROM forms
LEFT JOIN records ON forms.form_id = record.form_id
FULL OUTER JOIN:
Will include all data from both table and unmatched will be null
Ex :
SELECT *
FROM forms
FULL OUTER JOIN records ON forms.form_id = record.form_id