Joining of two table on a column which includes null value - mysql

I have two table Table-A and Table-B as given in picture.I want to join Table-A and Table-B on the column UIN.In table-A some records in column UIN contains null value. I want to include those records.My output should look like Table-C.I tried it by join these table but it does not including the receord which have null value of UIN in Table-A. Can any one help me out
Click here for table

you can either use full outer join or left join, depending on what you want to do when there are ids in Table B which are not present in Table A
select *
from A full outer join B
on A.UIN = B.UIN
or
select *
from A left join B
on A.UIN = B.UIN

Related

JOIN or Union of two tables - SQL

How can I unite two select statement in one table result?
For instance in the first table I want to get everything however on my 2nd table I only want the corel name that is equal to the corel_id and id of my 2nd table?
SELECT *
FROM garage
UNION
SELECT c.name
FROM corel as c
WHERE EXISTS (SELECT 1 FROM garage as g WHERE c.id = g.corel_id
I tried to execute this but this did not work. Is this right? or is there a better way to do this?
Sorry newbie here.
UPDATE EXPECTED RESULT :
https://anotepad.com/notes/b6662w
Give this a try:
SELECT g.*, c.name
FROM garage g
LEFT JOIN corel c
ON c.id = g.corel_id
Matching two tables in a database is called a join. An inner join, the default, returns only the rows that match from both tables.
A left join returns all the rows from the first table whether or not they match the second, and any data from the second table that matches. The right join does the inverse, returning only non-matching data from the second table. There is also the full join that returns all data regardless of match.
A join statement is what you need. A join puts columns from multiple tables into rows together based in the matching conditions in the where clause.
A union requires 2 or more queries to have the same columns. The union puts the sets of rows together into a longer set or rows.

mysql join 2 tables, second table needs SUM()

I have 2 tables,
first contains articles, second contains quantities. the two tables are linked with the "kodex" column. The second table can have multiple records for the same item, but can also have none.
what I need is a query that lists the entire first table, and adds an additional column that contains the sum of all quantities of all entries in the second table.
I have done this with left join on the kodex table, and works fine, but only as long as i do not add the sum() on the select statement. as soon as I do so, it lists only the rows that have a match on the second table.
Query that displays all rows:
SELECT b.* FROM `bestehend` as b left join eingelesen as e on e.kodex=b.kodex
query that displays only rows with matching entries in the second table:
SELECT b.*, sum(e.menge) as gesmenge FROM `bestehend` as b left join eingelesen as e on e.kodex=b.kodex
what I would need is the behaviour of the first query, with the additional column gesmenge from the second query.
thanks!
Update your query
SELECT b.*, sum(e.menge) as gesmenge FROM `bestehend` as b left join eingelesen as e on e.kodex=b.kodex
with adding group by b.article_id. Group by is needed so query know how to summarize all quantities of all entries in the second table.
Final query should look like
SELECT b.*, sum(e.menge) as gesmenge FROM `bestehend` as b left join eingelesen as e on e.kodex=b.kodex group by b.article_id

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

How to create the inner query wtih 5 different tables mysql

Having 5 tables
Table a_dates = id,
Table b_types = id, a_date_id, c_type_id,
Table c_types = id, name,
Table d_profiles = id, name, profile_type
Table e_ps = id, a_date_id, d_profile_id
From a_dates Need to get b_types,...then from b_types needs c_types name,... Then compare c_types name with d_profiles name and get d_profiles id.... if equals then create a records in e_ps with a_date_id, d_profile_id.
Could any one please help me in getting the query from inner join.
I tried like, it is incomplete query
INSERT INTO e_ps(id,a_date_id,a_date_type,d_profile_id,c_id)
SELECT '',a.id,'A',dp.id,'67' FROM d_profiles dp
INNER JOIN a_dates a ON {HERE I NEED NAME MATCHING WITH c_types name} = dp.name and dp.profile_type = 'A'
INNER JOIN a_dates ON a.id = a_dates.id
LEFT OUTER JOIN e_ps eps ON eps.a_date_type = 'A' AND eps.a_date_id = a_dates.id
WHERE eps.a_date_id IS NULL
This seems to be a relatively simple JOIN:-
INSERT INTO e_ps(id, a_date_id, d_profile_id)
SELECT NULL, a_dates.id, d_profiles.id
FROM a_dates
INNER JOIN b_types ON a_dates.id = b_types.a_date_id
INNER JOIN c_types ON b.c_type_id = c.id
INNER JOIN d_profiles ON c_types.name = d_profiles.name
With joins there are several types, and I suspect you are getting confused. Briefly:-
With an INNER JOIN it looks for a match that is on BOTH tables. If no
match the no record is returned.
With a LEFT OUTER JOIN it takes a record from the table on the left
and looks for a match on the table on the right. If a match great,
but if not then it still brings a row back but the columns from the
table on the right just have values of NULL.
A RIGHT OUTER JOIN is very much the same, just with the tables
reversed (most people including me avoid using this as it has no
advantages most of the time but just makes things confusing).
With a FULL OUTER JOIN it gets the records from both side, whether
they match or not. If they match then the columns from both are
returned, if not matched then the columns from one are returned. Not
that MySQL does not support a FULL OUTER JOIN (although there are
ways to emulate it).
A CROSS JOIN joins every combination of 2 tables. These are used when
there is no common column to match on but you want all combinations.
For example if you wanted a table of all employees and all days of
the week for each employee you would cross join a table of days of
the week against a table of employees (then for useful data you might
LEFT OUTER JOIN a table of holidays to the result).

MySQL: Return row if joined table contains least one match

(Main table id equal to jid. Join based on that.)
The 1st item has got 2 row in the join table. /That's great./
But 3rd item has got no row in join table.
The question: How can i ignore those items that has got no joined rows? IN ONE QUERY.
I tried the following:
SELECT *
FROM mainTable AS mainT
LEFT JOIN joinTable AS joinT ON mainT.id=joinT.jid
WHERE COUNT(joinT.id) > 0
Replace LEFT JOIN with INNER JOIN, and remove the WHERE clause.