Inner join query returning empty set - mysql

I have two tables with varchar columns having common values.
I am trying to perform a simple inner join on the tables on those varchar columns but I am getting empty set as a result.
There are null values present in these columns.
Collation for both columns are same.
I googled for it but cant find a solution.
The query is a simple join query and doesn't have any syntax issues.
When I change the inner to left join I get a resultset but all the columns in the right table are empty even for the rows passes the join condition.
I created two dummy tables with a common varchar to run a join query and it works.
So it is not a bug.So what are the possible reasons for getting empty sets any guesses.
The server is mysql version 5.5.37
update a set a.id=x where a.col in(Select col from b where .....);
Another issue is that i have a query like above but when I run it It kind of hangs...On running
SHOW PROCESSLIST;
the status is sending data...
What does it mean?
Can anyone help me.
Edit 1
The query is like this
UPDATE table1 bo INNER JOIN table2 ae
ON bo.joincol=ae.joincol AND ae.criteriaFieldName='UNSPSC' AND ae.taskId=4 AND ae.batchId = 44
SET bo.taskid=ae.taskId WHERE bo.procesId = 44 AND (taskid IS NULL OR taskid = 0);
Table1 contains 50+ columns
Table2 contains 8 columns
Unfortunately I can't disclose the data.
Interesting fact is if I join them using taskId I get a resultset.

For your second issue, try doing it as a JOIN instead of using IN:-
UPDATE a
INNER JOIN b
ON a.col = b.col
WHERE ........
SET a.id=x

Related

Why is this query returning only the first 11 rows only?

SELECT *
from projects
where projects.project_code in (select distinct_code from only_project_code where distinct_code is not null)
There are 84 matching values between distinct_code and project_code. But this query is returning only 11. Why?
Tables are here. Notice the query run on them.
That because of table projects may contains unique values, if you want duplicate you need a JOIN :
select p.project_code, pc.distinct_code
from projects p inner join -- you may need LEFT JOIN instead
only_project_code pc
on pc.distinct_code = p.project_code;

How would I return all null values from a left join?

I am trying to left join two tables (A and B) together and would like to return all values that have a column in table B marked as null in mySQL.
The two tables I am joining are both very large and I am running into an issue where my connection will timeout after 6000 seconds due to the DBMS settings; is there a way to make this query run more efficiently?
Another bit of information: Even if I limit the query to 10 rows, it will still timeout and give me the error code listed below.
select *
from Table_A a
left join Table_B b
on a.field_X = b.field_X
where b.Field_X is null;
I am experiencing the following error code: "Error Code 2013. Lost Connection to MySQL server during query."
Side note: I am a new SQL user and may need to ask for clarification on some answers. Thank you in advance!
First, you only need columns from a because the b columns are all null. You can write this as:
select a.*
from Table_A a left join
Table_B b
on a.field_X = b.field_X
where b.Field_X is null;
Then for this query, I recommend an index on table_b(field_x).
Maybe something like this will work for you:
select * from table_A a where a.field_X not in (select field_x from table_B);
Try the other way around, and reduce your SELECT fields if you can:
select Table_B.Field_X
from Table_B b
left join Table_A a
on a.field_X = b.field_X
where a.Field_X is null;
Let SQL do its work they way it likes to do it :) To have a standard left join (where you are looking for rows that do NOT match on the right) is highly optimised if you have the correct indexes

Natural join works but not with all values

I can't understand whats happening...
I use two sql queries which do not return the same thing...
this one :
SELECT * FROM table1 t1 JOIN table1 t2 on t1.attribute1 = t2.attribute1
I get 10 rows
this other :
SELECT * FROM table1 NATURAL JOIN table1
I get 8 rows
With the NATURAL JOIN 2 rows aren't returned... I look for the missing lines and they are the same values ​​for the attribute1 column ...
It's impossible for me.
If anyone has an answer I could sleep better ^^
Best regards
Max
As was pointed out in the comments, the reason you are getting a different row count is that the natural join is connecting your self join using all columns. All columns are being compared because the same table appears on both sides of the join. To test this hypothesis, just check the column values from both tables, which should all match.
The moral of the story here is to avoid natural joins. Besides not being clear as to the join condition, the logic of the join could easily change should table structure change, e.g. if a new column gets added.
Follow the link below for a small demo which tried to reproduce your current results. In a table of 8 records, the natural join returns 8 records, whereas the inner join on one attribute returns 10 records due to some duplicate matching.
Demo
You need to 'project away' the attribute you don't want used in the join e.g. in a derived table (dt):
SELECT *
FROM table1
NATURAL JOIN ( SELECT attribute1 FROM table1 ) dt;

MySQL - Records that don't exist in another table - showing incorrect results

This is my first post here.
I'm trying to display records from one table into another where they do not already exist using an outer join.
SELECT a.fname,a.lname,a.dob_month,a.dob_year
FROM new_person a LEFT JOIN person b
USING (fname,enter code herelname,dob_month,dob_day,dob_year,gender,zipcode)
WHERE (b.fname is null and b.lname is null and b.dob_year is null and b.dob_month is null)
The issue is, when I tested this query it it still shows me records that DO exist. I believe it has to do with the zipcode being null though i don't know why..
The "new_person" table only has 2 records that don't exist in the "person" table, but when i run this query it gives me 111 records.
However, if i remove the zipcode from the join clause it shows me the correct result of 2 records.
Any help would be appreciated.
You can try using EXISTS like
SELECT a.fname,
a.lname,
a.dob_month,
a.dob_year
FROM new_person a
WHERE NOT EXISTS (
SELECT 1 FROM person WHERE fname <> a.fname);
(OR) even using LEFT JOIN what you have already tried
SELECT a.fname,
a.lname,
a.dob_month,
a.dob_year
FROM new_person a
LEFT JOIN person b ON a.fname = b.fname
WHERE b.fname is null;
Again, I see that you are joining on all the columns. Not sure why? Is there any relationship exists between the tables? If so, then try joining on that related column rather.

Case statement in LEFT OUTER JOIN slowing the query in SQL Server 2008

I am getting a problem with my LEFT OUTER JOIN. I have a set of queries which gives me about 80,000 to 1,00000 records in a #Temp Table. Now when I LEFT OUTER JOIN this #Temp table with another table I have to put a CASE statement i.e. if the records are not found when joining with a particular column then take that particular column value and find its subsequent value in another table which has the matching records. The query is working fine for a particular data but for larger data it just goes on executing or just takes too much time. My query is like:
SELECT * FROM #Temp
LEFT OUTER JOIN TABLE1 ON #Temp.Materialcode =
CASE WHEN TABLE1.MaterialCode LIKE 'HY%'
THEN TABLE1.MaterialCode
ELSE REPLACE(TABLE1.MaterialCode,
TABLE1.MaterialCode,
(SELECT NewMaterialCode
FROM TABLE2
WHERE OldMaterialCode = TABLE1.MaterialCode))
END
Here TABLE2 has got only two columns NewMaterialCode and OldMetarialCode. What I have to do is if the Material Code is not found in TABLE1 LIKE 'HY%' type then it should take that material code and look for its subsequent NewMaterialCode in TABLE2 to get both types of records having 'HY' type and non 'HY' type. I think I made my problem clear. Any help would be greatly appreciated.
SELECT *
FROM #TEMP TMP
LEFT JOIN Table1 MATERIAL
ON TMP.MaterialCode = MATERIAL.MaterialCode
LEFT JOIN Table2 REPLACEMENT
ON MATERIAL.MaterialCode = REPLACEMENT.OldMaterialCode
WHERE ( COALESCE(MATERIAL.materialcode, '') LIKE 'HY%'
AND TMP.materialCode = MATERIAL.MaterialCode
)
OR MATERIAL.MaterialCode = REPLACEMENT.NewMaterialCode
I think this should do what you're trying to do, but I don't really know how the tables are related except by reverse-engineering your query.
For the record, the OUTER JOIN in your query isn't accomplishing a thing, because an outer condition would product null values for the columns in TABLE1, and the case condition wouldn't work (a NULL would be neither a match for 'HY%' nor an ELSE). That's counter-intuitive to those not used to working in the three-valued logic of the database world, but that's why we have COALESCE and ISNULL.