Selecting Fields and Join Clause - sql

I have two tables, TableA:
- Original_Location
- Units
and TableB:
- Original_Loc
- Adjacent_Loc
- Direction (up/down/left/right from original loc)
My goal is to return the original location, the adjacent location, the direction, and the number of units at the adjacent loc. So far, I've only been able to return the units from the original loc.
Here is what I've tried so far:
Select Original_Location,
Units,
TableB.Adjacent_Loc,
TableB.Direction
From TableA
Inner Join
Select *
From TableB
On TableA.Original_Location = TableB.Original_Loc
My thought is that I might need to change the fields I'm selecting before the inner join, or potentially join on Original_Location = Adjacent_Loc.

Your Join syntax is not right. First you don't need to "select" anything from Table B, your initial SELECT is getting data from the joined tables (A and B) as if they were 1 table. Secondly, you need to specify which fields to join the tables with.
Your join will be something like :-
From TableA Inner Join TableB
on TableA.Original_Loc = TableB.Original_Loc
Once you have got your joins right, you need to make another join to TableA to get the Units. This time you are joining the Adjacent_Loc in Table B to the Original_Location in Table A - which will have the Units value you need.
My example below uses aliases to identify each table (there are now 2 references to TableA so they need to be identified separately). So when you do the second join to TableA, this has the alias of c to differentiate it from the first TableA reference. You then need to select the Units from c.
Select a.Original_Location, c.Units, b.Adjacent_Loc, b.Direction
From TableA a Inner Join TableB b On a.Original_Location = b.Original_Loc
inner join TableA c on b.Adjacent_Loc = c.Original_Location

Related

SQL - Need to conditionally join based on value in TableB column

I need to know the rows in TABLE A that have join records in TABLE B based a column value in TABLE B, but I also need to return rows in which a row in TABLE A has no match in TABLE B.
It seems like I need a LEFT JOIN and a LEFT OUTER JOIN, so I'm not sure what to do there. I understand how to do each, but don't understand how to do them together.
The schema looks like:
TABLE_A
pk
TABLE_B
pk
a_fk
some_value
I need the joined rows where Table_A has no join record in Table_B OR Table_A has a join record row in Table_B (it can have many) in which some_value does not equal "thisValue"
Thanks.
A Left join is a left outer join. Outer joins preserve one of the tables which is what you are after so good guess.
SELECT *
FROM Table A
LEFT JOIN Table B
ON TableA.Column = TableB.Column
AND B.SomeValue <> 'ThisValue'
All of the rows with a match will have the B information populated all of those without will have nulls in the B data

Update records only once with LEFT OUTER JOIN

I am trying to UPDATE Table A with records in Table B based on a LEFT OUTER JOIN based on two columns of data. So, for example, the JOIN from Table A might be USA-4 to match Table B's USA-4. Returning a result of XYZ to a different column. The problem I am having is the Table B contains multiple USA-4's and as a result Table A appears to be updating more than once. Is there a way to fix this? A different JOIN type?
UPDATE tablea a
SET USA-4 = (SELECT distinct USA-4 FROM tableb b WHERE a.id=b.id)

Having problems with SQL Joins

Table A
Table B
I tried to use LEFT OUTER JOIN but it seems not working..
I want the query to extract all data from Table A with 0 as average score if there is no data yet for the specified parameter. Meaning, in Figure 3, it should have shown ID 2 with 0 on s. Can anyone help me figure out the solution?
You have the table names switched in the join. To keep all of Table A then it needs to be the table listed on the left side of the left join. Also anything that you want to only affect the output of table B, and not filter the entire results, should be moved to the left join on clause. Should be:
SELECT a.id,
Avg(Isnull(b.score, 0)) AS s
FROM a
LEFT OUTER JOIN b
ON a.id = b.id
AND b.kind = 'X'
GROUP BY a.id

Select multiple tables when one table is empty in MySQL

I'm trying to do
SELECT * FROM a, b
However, it doesn't return anything if one of the tables is empty. How do I make it so it returns 'a' even if the other one is empty?
Using two tables in the from clause is functionally equivalent to a cross join:
select *
from A
cross join
B
This returns a row of A for every row in B. When B is empty, the result is empty too. You can fix that by using a left join. With a left join, you can return rows even if one of the tables is empty. For example:
select *
from A
left join
B
on 1=1
As the condition 1=1 is always true, this is just like a cross join except it also works for empty tables.
SELECT * FROM a LEFT JOIN b ON a.ID = b.ID
Will return everything from a even if b is empty.
You should do a left join.
Like this
SELECT *
FROM A
LEFT JOIN B ON A.ID = B.ID
Then you receive the rows in A and the respective row in B if exists.
SELECT a.*, b.* FROM a LEFT JOIN b ON a.id = b.id
in this example id is just example name for join key
The query mentioned above display join of both tables if a contain 2 record and b contain 7 records it displays 7*2 = 14 records. In your case one of the table is empty( with 0 records), it will not display any data. If still you want to display data and tables are not having any relationship, you need to check if count of both tables greater that 0. Otherwise display records from only one table which is not empty.

How to make a one to one left outer join?

I was wondering, is there a way to make a kind of one to one left outer join:
I need a join that matches say table A with table B, for each record on table A it must search for its pair on table B, but there exists only 1 record that matches that condition, so when it has found its pair on B, it must stop and continue with the next row at table A.
What I have is a simple LEFT OUTER JOIN.
select * from A left outer join B on A.ID = B.ID order by (NAME) asc
Thanks in advance!
SQL doesn't work this way. In the first place it does not look at things row-by-row. In the second place what defines the record you want to match on?
Assuming you don't really care which row is selcted, something like this might work:
SELECT *
From tableA
left outer join
(select b.* from tableb b1
join (Select min(Id) from tableb group by id) b2 on b1.id - b2.id) b
on a.id = b.id
BUt it still is pretty iffy that you wil get the records you want when there are multiple records with the id in table b.
The syntax you present in your question is correct. There is no difference in the query for joining on a one-to-one relationship than on a one-to-many.