I have a table A -
SNo ID Place
1 1000 Null
2 Null Null
3 1020 CityX
And another table B -
ID Place
1000 CityY
2000 CityZ
4040 CityAA
Now, I need to join table A and B such that I can get the values of Place in table A from table B. So my final table should look like this -
SNo ID Place
1 1000 CityY
2 1020 CityX
I'm trying to create an SQL query with joins, but that is only giving me empty rows. I did -
Select * from A
left outer join B
on A.ID = B.ID
where A.ID IS NOT NULL
Where is my query breaking? How do I get the expected result?
Select A.Sno, A.ID, IF(A.Place is null, B.Place, A.place) as Place from A
left join B
on A.ID = B.ID
where A.ID IS NOT NULL
Seems you need inner join
Select A.ID, ifnull(A.Place, B.Place) from A
Inner join B
on A.ID = B.ID
where A.ID IS NOT NULL
Technically, there is nothing wrong with your query, here is a working sqlfiddle of your question
http://sqlfiddle.com/#!9/29a002/1/0
Since you didn't post your actual schema or screenshot of it, I am going to suspect incompatible column definitions or invalid data
You can just do it like this Select the Place field of table B instead.
select SNo, B.ID, B.Place
from A
left outer join B
on A.ID = B.ID
Related
I have 2 mysql tables a and b. I need to show all records in a that are not in b using a common column 'ID'. Normally this is pretty straight forward but my problem here is this: I need to put a where clause on table b because I'm not interested in all unmatched records just the ones that meet the table 2 where clause criterion:
SELECT a.ID, a.Description
FROM a
LEFT JOIN b ON a.ID = b.ID
WHERE a.Inactive = 0
AND b.Room = '101'
AND b.ID Is Null
This returns nothing. However if I remove the AND b.Room = '101' part, it displays the expected results. But I need that condition because I need only unmatched records specific to a 'room' and not all unmatched records
Move the conditions involving table b from WHERE to ON clause:
SELECT a.ID, a.Description
FROM a
LEFT JOIN b ON a.ID = b.ID AND b.Room = '101'
WHERE a.Inactive = 0
AND b.ID Is Null
It'll find rows where a does not have a match in b (id matches and room number = 101).
I have two tables. A and B
SELECT projectid, statusid from A;
Results as below:
SELECT statusid, status from B
Results as below:
Now how can I have the result as below to replace all stutusid with the value from table B.
You could use JOIN:
SELECT A.projectid, B.status from A JOIN b ON A.statusid = b.statusid;
EDIT:
I want to have all those status codes replaced by "its meaning" which is from another table B. So "join" wont do this job but only showing me matched results from both A and B table
I don't understand the requirements. This is exactly what join is doing, showing corresponding value from second table. If for example you don't have the specific code you should use LEFT OUTER JOIN
SELECT A.projectid, COALESCE(B.status, 'Unknown') AS status
from A
LEFT JOIN b ON A.statusid = b.statusid;
Maybe you are searching for correlated subquery(but it will be literally the same as JOIN) if there is no duplicates on table B
SELECT A.projectid,
(SELECT B.status FROM B WHERE A.statusid = b.statusid) AS status
FROM A
-- if it returns error query returned more than one row then b.statusid is not unique
SELECT A.projectid,
(SELECT B.status FROM B WHERE A.statusid = b.statusid ORDER BY ... LIMIT 1) AS status
FROM A
I am building a SQL query which compares two tables A and B by a [name] column and returns the names from table A that are not in table B
Example
Table A
ID Name Address
1 A ABC
2 B XYZ
3 C PQR
Table B
ID Name Gender
1 A F
2 B M
3 D F
The query I wrote should return third row from table A as it is not in table B and should exclude all other rows
Following is the query I built
Select * from A oa left join B gp ON oa.name!=gp.name
the above doesn't return the results I was expecting.
Can this be corrected?
Easiest way:
select * from A where name not in (select name from B)
Better way:
select * from A where not exists (select 1 from B where B.name = A.name)
"A left join B" means keeping everything in A, and associating records in B if the condition is satisfied.
In your case, if you really wanna use left join, here is what it should be ('=', not '!='):
Select * from A oa left join B gp ON oa.name=gp.name where gp.name is null
Better way would be using 'not exists' performance-wise, or 'except' if null values are not an issue.
Using excpet operator will help
select * from TableA
except
select * from TableB
SELECT a.*
FROM A a
LEFT JOIN B b
ON a.name = b.name
WHERE b.name IS NULL
I've this table (a)
And this table (b)
Now I have to get all records from A which are not present in B (a.id not present as b.idDomanda) and where B.idUser is not 1. So In this case, it should return only id 2 from a, but it returns 1 and 2.
This is my Query
SELECT a.* FROM a LEFT JOIN b ON a.id=b.idDomanda WHERE ( b.idUser <> 1 OR b.idUser IS NULL ) GROUP BY a.id
You want to move the condition on b to the on clause:
SELECT a.*
FROM a LEFT JOIN
b
ON a.id = b.idDomanda and b.idUser <> 1
WHERE b.idUser IS NULL
GROUP BY a.id;
The group by suggests that you might want to use not exists instead:
select a.*
from a
where not exists (select 1
from b
where a.id = b.idDomanda and b.idUser <> 1
);
There should be no results given your data set.
All records from A which are not present in B (a.id not present as b.idDomanda)
Given the test data set all of A is in fact IN b.idDomanda... even when filtering out userId = 1.
but as the previous person pointed out that is the query to check.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Full Outer Join in MySQL
There are two tables tableA and table B
both the tables have common column id
We want to get the results which are having all records in A but not in B
and all records which exists in B but not in A
Regards,
Chinta kiran
You can use UNION operator as follow
SELECT * FROM tablea
UNION
SELECT * FROM tableb
if you want to read more about
UNION operator
This is best accomplished with a LEFT OUTER JOIN where the predicate (WHERE clause) ensures that the joined row is NULL; something like:
SELECT A.* FROM A LEFT OUTER JOIN B ON A.id = B.a_id WHERE B.a_id IS NULL;
I suggest you to read the article A Visual Explanation of SQL Joins by Jeff Atwood of Coding Horror.
Full outer join produces the set of all records in Table A and Table B, with matching records from both sides where available. If there is no match, the missing side will contain null.
You are looking for MINUS SET OPERATOR:
"We want to get the results which are having all records in A but not in B"
Easy way:
SELECT A.*
FROM A
WHERE A.id not in (SELECT id FROM B)
With Full Outer Join
SELECT A.*
FROM A full outer join B on A.id = B.id
WHERE B.id is Null
The right way:
SELECT A.*
FROM A left outer join B on A.id = B.id
WHERE B.id is Null
Change A to B and B to A for in order to get the results which are having all records in B but not in A.