MySQL left join, less results - mysql

I've got a little problem, I'm stuck with. I got two tables. The first one holds all id's, the second one some, but not all from tableA, a value and a corresponding identifier. As I thought I had it understood, a left join on the id's should give me all id's in the first table and null for the second, if the id doesn't exist in tableB. But I keep getting the number of id's that exist in both tables. Did I get the concept wrong?
My statement so far is:
SELECT tableA.id, tableB.desiredValue
FROM tableA
LEFT JOIN tableB ON tableA.id=tableB.item_id
WHERE tableB.element_id = 'something'
OR tableB.element_id IS NULL;
Any pointers?
Thanx, best regards,
Marcus

Move the condition that involves tableB column from WHERE to ON:
SELECT tableA.id, tableB.desiredValue
FROM tableA
LEFT JOIN tableB ON tableA.id=tableB.item_id
AND tableB.element_id = 'something'
--- WHERE tableB.element_id IS NULL --- this is probably not appropriate
;

Try this
SELECT tableA.id, tableB.desiredValue
FROM tableA
LEFT JOIN tableB ON tableA.id=tableB.item_id
AND (tableB.element_id = 'something'
OR tableB.element_id IS NULL);

In general, this seems like it should work. I'd be more specific in the where clause, however:
SELECT tableA.id, tableB.desiredValue
FROM tableA
LEFT JOIN tableB ON tableA.id = tableB.item_id
WHERE tableB.element_id = 'something' OR tableB.item_id IS NULL;
I've substituted item_id as the row I'm checking for NULL in -- though it should make a difference unless element_id can be NULL in rows where the join works.
Can you expand on why you want the other portion of the where clause -- tableB.element_id = 'something' ? My general recommendation if you just want all rows returned would be to eliminate the WHERE clause entirely.
Can you explain a bit more what the criteria are for the rows you want returned in general?

Related

MySQL: Trouble trying to get my SQL statement to work when condition is true but can also accept false

I have the following statement:
SELECT tableA.*, tableB.* FROM tableA, tableB WHERE tableA.userUUID = ? AND tableB.uuid=tableA.tableBUUID
So when a record in table A has a valid tableBUUID and the tableBUUID exists in tableB then the query runs file. But when I removed the tableBUUID from table B without removing it from the record in tableA then the query returns nothing (Which is obviously expected)
I am wondering how I can change the query so that even if the tableBUUID does not match anything, the query will still return what it can even if its nothing/ blank columns from tableB?
Thanks all
You can achieve this via a LEFT JOIN:
SELECT tableA.*, tableB.*
FROM tableA
LEFT JOIN tableB ON tableB.uuid = tableA.tableBUUID
WHERE tableA.userUUID = ?
Fiddle.
Performing a LEFT JOIN will return data from tableA and matching records from tableB if it exists in tableB, otherwise it'll return only the records from tableA.
In the syntax you posted above in your question, you're essentially doing a INNER JOIN by comma separating your tables. This is the old syntax, moving forward you should explicitly write INNER JOIN like so:
SELECT * FROM tableA a INNER JOIN tableB b ON a.column_key = b.column_key
Read more about it in the following previous question:
What's the difference between comma separated joins and join on syntax in MySQL?

MYSQL - using AND in JOIN versus WHERE clause

If I have this query:
select * from tableA
left outer join tableB on tableA.id=tableB.id
AND tableB.foo = 1
where tableA.owner=10
I get 29 results, but if I move that AND into the WHERE clause like:
select * from tableA
left outer join tableB on tableA.id=tableB.id
where tableA.owner=10
AND tableB.foo = 1
I then get only 17 results.
I've looked all around and cannot find a definitive guide as to how using the AND differs when you use it in the JOIN versus the WHERE clause. Can anyone explain this to me?
Also, if I do something like AND tableB.foo = NULL in the JOIN all of my tableB.foo fields are NULL in the query results, even if they are not null in the table. Does having the AND in the JOIN clause change that field in the FROM selection before being filtered by the WHERE clause?
All of the criteria for the table you are outer-joining to should be in the JOIN clause (like your first query). Putting it in the WHERE clause (like your second query) implicitly converts the OUTER JOIN to an INNER JOIN.
As for your question about AND tableB.foo = NULL that is not proper MySQL syntax. NULLs require special treatment, using operators like IS NULL. You should use AND tableB.foo IS NULL instead.
An outer join joins just the same as an inner join. With the addition that when there is no match for a record, a dummy record with all columns set to null gets joined (so you still get the row from the first table in your results).
In your first query you are looking for matches in tableB with the same ID and foo = 1. For records in tableA with no such match you still get a result row (with all tableA fields null).
In the second query you are looking for matches in tableB with the same ID. For records in tableA with no such match you still get a result row (with all tableA fields null). Then in your where clause you only keep rows with foo = 1. This dismisses all outer-joined records (because their foo is null) and you are where you would have been with a plain inner join.
So always put all criteria on an outer-joined table in the ON clause. (There is one exception though; an anti join, but you can learn that pattern another time.)

Order of keys in the LEFT JOIN condition

What happens when we reverse the order of keys in the LEFT JOIN condition?
Ideal SQL left join syntax for joining two tables is
select fields
from tableA
LEFT JOIN tableB
ON tableA.key = tableB.key
Flipped order of join condition
select fields
from tableA
LEFT JOIN tableB
ON tableB.key = tableA.key -- (order flipped)
Will these queries produce different set of results?
You will get the same results. The on clause just evaluates to true or false; the order does not matter.
Only the position of the tables themselves matter. e.g. reversing table order as below would give different results.
select fields
from tableB
LEFT JOIN tableA
ON tableA.key = tableB.key
LEFT JOIN is not commutative, you can't switch arguments: tableA LEFT JOIN tableB is equivalent to tableB RIGHT JOIN tableA, and different from tableB LEFT JOIN tableA.
== is commutative, you can switch arguments: tableA.key = tableB.key is equivalent to tableB.key = tableA.key.
No any difference, except readability of the query.
SQL was designed to be readable as usual English text. In a usual text, when we want to specify any mentioned object, we say something like "An object. This object is like that object, this object is related to that object, and this object has those properties." We do not say "That object is like this object, to that object related this object and those properties have this object".
So, when we adding records of TableB to query from TableA, we need to specify what must have a record from TableB to satisfy current record from TableA. So we must say, wee need that records from TableB, which have an id, equal to TableA.ID. TableB.id = TableA.id. We must write exactly in this order, to provide easy reading and understanding of this relation.

MySQL - Selecting rows where fields not equal

I have a little problem with an SQL query: I have 'TableA' with a field 'TableA.b' that contains an ID for 'TableB'. I want to select all rows from 'TableB' that don't have an ID that equals any field 'TableA.b'. With other words, I need every row from TableB that's not referred to by any row from TableA in field .
I tried a Query like this :
SELECT DISTINCT TableB.* FROM TableA, TableB Where TableA.b != TableB.ID
But the result contains a row that is also returned by the negation, i.e. where both fields have the same value.
Any ideas?
What you need is LEFT (or RIGHT) JOIN.
SELECT TableB.* FROM TableA
LEFT JOIN TableB on TableA.b = TableB.ID
WHERE TableA.b IS NULL
While it's possible to do the same with a subquery as in some of the otehr answers. A join will often be faster.
A LEFT [OUTER] JOIN can be faster than an equivalent subquery because
the server might be able to optimize it better—a fact that is not
specific to MySQL Server alone. Prior to SQL-92, outer joins did not
exist, so subqueries were the only way to do certain things. Today,
MySQL Server and many other modern database systems offer a wide range
of outer join types.
First, select all ids from TableA:
SELECT DISTINCT b FROM TableA
Then use that result to select all rows in TableB that have an id that does not exist in this set by using the above query as a subquery:
SELECT * FROM TableB WHERE ID NOT IN (SELECT DISTINCT b FROM TableA)
Hope this helps.
You can try this
SELECT TableB.* FROM TableB
WHERE ID NOT IN
(SELECT b from TableA);
Use NOT IN in SELECT Query.
SELECT * FROM TableB t1 WHERE t1.ID NOT IN (SELECT t2.b FROM TableA t2);
You can use right join also.
Try this:
SELECT DISTINCT TableB.* FROM tablea RIGHT JOIN TableB ON TableA.b = Tableb.ID WHERE TableA.B IS NULL

SQL outer join which omits common records

I'm trying to formulate an SQL FULL OUTER JOIN, which includes all values in table A and table B, but not those values common between them.
I have searched the internet, and stumbled upon the following SQL code:
SELECT * FROM TableA
FULL OUTER JOIN TableB
ON TableA.name = TableB.name
WHERE TableA.id IS null
OR TableB.id IS null
Which can be illustrated like so:
I'm not sure I understand the IS null parts. Could the SQL be carried out by simply stating something like the following as a WHERE condition? :
WHERE TableA.id <> TableB.id
What is it you don't understand about the IS NULL clauses?
In an OUTER JOIN (LEFT, RIGHT, FULL) there's a chance that columns from the outer table could end up as NULL.
The clauses
WHERE TableA.id IS null
OR TableB.id IS null
are simply saying that one of the IDs has to be NULL, I.E. if you have a row from TableA there can't exist a matching row from TableB and vice versa.
SELECT Name, ID FROM TableA UNION SELECT Name, ID FROM TableB
EXCEPT
SELECT Name, ID FROM TableB INTERSECT SELECT Name, ID FROM TableA
The first select gets all rows from table A and table B and combines this into 1 result set.
The second select selects all rows that are common between the two.
What the except does is select all rows from the first select - all rows from the second select.
What you end up with is all rows - the rows that are common between the two tables.