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.
Related
I'm trying to JOIN a Master Dataset, via a left join with 2 other Datasets, all of them have the same Key field. So nothing special there.
One of those secondary Datasets is the result of another Query and therefor might or might not exist. Obviously my JOIN statement fails when this table doesn't exist.
Below a really simplified version of the code, the JOIN is used to exclude rows from the table_a that exist in table b or c (if they exist).
SELECT a.id, a.name
FROM table_a a
LEFT JOIN table_b b
ON a.id = b.id
LEFT JOIN table c c
ON a.id = c.id
WHERE b.id IS NULL
AND c.id IS NULL;
I am not sure that I understand your question well, but I think that you should better do:
SELECT a.id,a.name
FROM table_a a
WHERE a.id NOT IN
(SELECT id FROM table_b)
AND a.id NOT IN
(SELECT id FROM table_c)
Any query optimizer should have the exact same performance with this request, and I find it much more readable.
Very often join fields have the same name in joined tables. If just join
SELECT a.*, b.* FROM a INNER JOIN b ON a.id=b.id
it will produce id field twice.
Is it possible to include ALL fields from joined table EXCEPT joined one?
UPDATE
I am using MySQL but standard way is also interesting to me!
UPDATE 2
Regarding USING syntax, how to use it with multiple joins?
SELECT * FROM
a INNER JOIN b USING (b_id)
INNER JOIN c USING (c_id)
swears table b doesn't contain c_id field, which is true, since it is inside a.
Normally I would write
SELECT * FROM
a INNER JOIN b ON a.b_id = b.b_id
INNER JOIN c ON a.c_id = c.c_id
In standard SQL this is achieved through USING
select *
from a
join b using (id);
This will return the id column only once.
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
I wanted to have a full outer join in memsql. Something like
SELECT *
FROM A FULL OUTER JOIN B
ON A.id = B.id
Is it possible ?
It appears that MemSQL does not have a FULL OUTER JOIN syntax. However, you should be able to simulate a full outer join in MemSQL using a combination of LEFT and RIGHT OUTER JOIN operations:
SELECT * FROM A
LEFT OUTER JOIN B ON A.id = B.id
UNION ALL
SELECT * FROM A
RIGHT OUTER JOIN B on A.id = B.id
WHERE ISNULL(A.id)
The first SELECT covers the orange area, namely matching records between A and B along with records in A which do not match to anything in B. The second query obtains only records in B which do not match to anything in A. Using UNION ALL instead of UNION ensures that duplicates are not removed.
I'm trying to improve a (not so much) simple query:
I need to retrieve every row from Table A.
Then join Table A with Table B so I get all the data I need.
At the same time, I need to add an extra column with the count() from Table C.
Something like:
SELECT a.*,
(SELECT Count(*)
FROM table_c c
WHERE c.a_id = a.id) AS counter,
b.*
FROM table_a a
LEFT JOIN table_b b
ON b.a_id = a.id
This works, ok, but in reality, I'm just making 2 queries and I need to improve this so it only do one (if, its even possible).
Anyone knows how can I achive that?
The simplest approach is likely to just move the correlated sub-query into a sub-query.
NOTE: Many optimisers deal with correlated sub-queries extremely effectively. Your example query could be perfectly reasonable.
SELECT
a.*,
b.*,
c.row_count
FROM
table_a a
LEFT JOIN
table_b b
ON b.a_id = a.id
LEFT JOIN
(
SELECT
a_id,
Count(*) row_count
FROM
table_c
GROUP BY
a_id
)
c
ON c.a_id = a.id
Another Note: SQL is an expression, it is not executed directly, it is translated into a plan using nest loops, hash joins, etc. Do not assume that having two queries is a bad thing. In this case my example may significantly minimise the number of reads compared to a single query and then use of GROUP BY and COUNT(DISTINCT).
Try this:
SELECT
tmp.*,
SUM(IF(c.a_id IS NULL,0,1)) as counter,
FROM (
SELECT
a.id as aid,
b.id as bid,
a.*,
b.*
FROM
table_a a
LEFT JOIN table_b b
ON b.a_id = a.id
) as tmp
LEFT JOIN table_c c
ON c.a_id = tmp.id
GROUP BY
tmp.aid,
tmp.bid