SQL is intermittently returning no data in response to a LEFT JOIN









up vote
0
down vote

favorite












I am trying to hunt down a bug in a SQL query but can't seem to get to the bottom of it. The query looks like this:



SELECT
DATE(BT.DateCheckedIn) AS X,
DAYOFWEEK(BT.DateCheckedIn) AS DayX,
SUM(IR.QtyCheckedIn) AS C,
AU.AdminUsername,
AU.AdminFirstName,
AU.AdminLastName,
IF(BTE.ProdLogID IS NULL, 'No', 'Yes') AS Exclude
FROM
buying_issuesreceived IR
JOIN buying_transactions BT ON IR.TransactionID = BT.TransactionID
JOIN adminusers AU ON BT.CheckedInByAdminUserID = AU.AdminUserID
LEFT JOIN log_production_bt BTE
ON DATE(BT.DateCheckedIn) = DATE(BTE.ProductionDate)
AND BTE.ProductionSection = 'wtransactions'
AND AU.AdminUsername = BTE.ProductionUsername
WHERE
DATE(BT.DateCheckedIn) BETWEEN DATE '2018-09-24' AND DATE '2018-09-30'
GROUP BY
DATE(BT.DateCheckedIn),
AU.AdminUsername


When this query is run, it has about a 50/50 chance of returning the correct data or returning nothing at all. There is no error message. I know, or rather should say, am pretty certain, that the LEFT JOIN is the culprit because when I delete it from the code I stop getting empty tables, but can't, for the life of me, figure out why this query would return inconsistent results in the first place.










share|improve this question























  • I see nothing wrong with the left outer join in your query. I'd prefer an IN or EXISTS clause in the select clause, but the left outer join should work just as fine. Maybe there is something wrong with the database? Try REPAIR TABLE or mysqlcheck --repair (dev.mysql.com/doc/refman/5.7/en/…).
    – Thorsten Kettner
    Nov 5 at 22:33







  • 2




    Usually it's an INNER JOIN that causes missing results in queries. It's hard for a LEFT JOIN to cause this issue.
    – Joel Coehoorn
    Nov 5 at 22:44










  • Yeah. It's a weird one. That's what pushed me to stack overflow. I've passed this to every other coder here and to my boss and everyone is scratching their heads. The database seems fine because we have a bunch of other queries calling these tables that work just fine. It's just one small subset of queries of this type, which don't seem to have anything wrong with them, that are having issues. Been banging my head against the wall all day.
    – Jeremiah
    Nov 5 at 22:50











  • What versions of MySQL, client & server? See Minimal, Complete, and Verifiable example.
    – philipxy
    Nov 11 at 19:15














up vote
0
down vote

favorite












I am trying to hunt down a bug in a SQL query but can't seem to get to the bottom of it. The query looks like this:



SELECT
DATE(BT.DateCheckedIn) AS X,
DAYOFWEEK(BT.DateCheckedIn) AS DayX,
SUM(IR.QtyCheckedIn) AS C,
AU.AdminUsername,
AU.AdminFirstName,
AU.AdminLastName,
IF(BTE.ProdLogID IS NULL, 'No', 'Yes') AS Exclude
FROM
buying_issuesreceived IR
JOIN buying_transactions BT ON IR.TransactionID = BT.TransactionID
JOIN adminusers AU ON BT.CheckedInByAdminUserID = AU.AdminUserID
LEFT JOIN log_production_bt BTE
ON DATE(BT.DateCheckedIn) = DATE(BTE.ProductionDate)
AND BTE.ProductionSection = 'wtransactions'
AND AU.AdminUsername = BTE.ProductionUsername
WHERE
DATE(BT.DateCheckedIn) BETWEEN DATE '2018-09-24' AND DATE '2018-09-30'
GROUP BY
DATE(BT.DateCheckedIn),
AU.AdminUsername


When this query is run, it has about a 50/50 chance of returning the correct data or returning nothing at all. There is no error message. I know, or rather should say, am pretty certain, that the LEFT JOIN is the culprit because when I delete it from the code I stop getting empty tables, but can't, for the life of me, figure out why this query would return inconsistent results in the first place.










share|improve this question























  • I see nothing wrong with the left outer join in your query. I'd prefer an IN or EXISTS clause in the select clause, but the left outer join should work just as fine. Maybe there is something wrong with the database? Try REPAIR TABLE or mysqlcheck --repair (dev.mysql.com/doc/refman/5.7/en/…).
    – Thorsten Kettner
    Nov 5 at 22:33







  • 2




    Usually it's an INNER JOIN that causes missing results in queries. It's hard for a LEFT JOIN to cause this issue.
    – Joel Coehoorn
    Nov 5 at 22:44










  • Yeah. It's a weird one. That's what pushed me to stack overflow. I've passed this to every other coder here and to my boss and everyone is scratching their heads. The database seems fine because we have a bunch of other queries calling these tables that work just fine. It's just one small subset of queries of this type, which don't seem to have anything wrong with them, that are having issues. Been banging my head against the wall all day.
    – Jeremiah
    Nov 5 at 22:50











  • What versions of MySQL, client & server? See Minimal, Complete, and Verifiable example.
    – philipxy
    Nov 11 at 19:15












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to hunt down a bug in a SQL query but can't seem to get to the bottom of it. The query looks like this:



SELECT
DATE(BT.DateCheckedIn) AS X,
DAYOFWEEK(BT.DateCheckedIn) AS DayX,
SUM(IR.QtyCheckedIn) AS C,
AU.AdminUsername,
AU.AdminFirstName,
AU.AdminLastName,
IF(BTE.ProdLogID IS NULL, 'No', 'Yes') AS Exclude
FROM
buying_issuesreceived IR
JOIN buying_transactions BT ON IR.TransactionID = BT.TransactionID
JOIN adminusers AU ON BT.CheckedInByAdminUserID = AU.AdminUserID
LEFT JOIN log_production_bt BTE
ON DATE(BT.DateCheckedIn) = DATE(BTE.ProductionDate)
AND BTE.ProductionSection = 'wtransactions'
AND AU.AdminUsername = BTE.ProductionUsername
WHERE
DATE(BT.DateCheckedIn) BETWEEN DATE '2018-09-24' AND DATE '2018-09-30'
GROUP BY
DATE(BT.DateCheckedIn),
AU.AdminUsername


When this query is run, it has about a 50/50 chance of returning the correct data or returning nothing at all. There is no error message. I know, or rather should say, am pretty certain, that the LEFT JOIN is the culprit because when I delete it from the code I stop getting empty tables, but can't, for the life of me, figure out why this query would return inconsistent results in the first place.










share|improve this question















I am trying to hunt down a bug in a SQL query but can't seem to get to the bottom of it. The query looks like this:



SELECT
DATE(BT.DateCheckedIn) AS X,
DAYOFWEEK(BT.DateCheckedIn) AS DayX,
SUM(IR.QtyCheckedIn) AS C,
AU.AdminUsername,
AU.AdminFirstName,
AU.AdminLastName,
IF(BTE.ProdLogID IS NULL, 'No', 'Yes') AS Exclude
FROM
buying_issuesreceived IR
JOIN buying_transactions BT ON IR.TransactionID = BT.TransactionID
JOIN adminusers AU ON BT.CheckedInByAdminUserID = AU.AdminUserID
LEFT JOIN log_production_bt BTE
ON DATE(BT.DateCheckedIn) = DATE(BTE.ProductionDate)
AND BTE.ProductionSection = 'wtransactions'
AND AU.AdminUsername = BTE.ProductionUsername
WHERE
DATE(BT.DateCheckedIn) BETWEEN DATE '2018-09-24' AND DATE '2018-09-30'
GROUP BY
DATE(BT.DateCheckedIn),
AU.AdminUsername


When this query is run, it has about a 50/50 chance of returning the correct data or returning nothing at all. There is no error message. I know, or rather should say, am pretty certain, that the LEFT JOIN is the culprit because when I delete it from the code I stop getting empty tables, but can't, for the life of me, figure out why this query would return inconsistent results in the first place.







mysql sql database join left-join






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 12:58









Zoe

10.7k73575




10.7k73575










asked Nov 5 at 22:22









Jeremiah

536




536











  • I see nothing wrong with the left outer join in your query. I'd prefer an IN or EXISTS clause in the select clause, but the left outer join should work just as fine. Maybe there is something wrong with the database? Try REPAIR TABLE or mysqlcheck --repair (dev.mysql.com/doc/refman/5.7/en/…).
    – Thorsten Kettner
    Nov 5 at 22:33







  • 2




    Usually it's an INNER JOIN that causes missing results in queries. It's hard for a LEFT JOIN to cause this issue.
    – Joel Coehoorn
    Nov 5 at 22:44










  • Yeah. It's a weird one. That's what pushed me to stack overflow. I've passed this to every other coder here and to my boss and everyone is scratching their heads. The database seems fine because we have a bunch of other queries calling these tables that work just fine. It's just one small subset of queries of this type, which don't seem to have anything wrong with them, that are having issues. Been banging my head against the wall all day.
    – Jeremiah
    Nov 5 at 22:50











  • What versions of MySQL, client & server? See Minimal, Complete, and Verifiable example.
    – philipxy
    Nov 11 at 19:15
















  • I see nothing wrong with the left outer join in your query. I'd prefer an IN or EXISTS clause in the select clause, but the left outer join should work just as fine. Maybe there is something wrong with the database? Try REPAIR TABLE or mysqlcheck --repair (dev.mysql.com/doc/refman/5.7/en/…).
    – Thorsten Kettner
    Nov 5 at 22:33







  • 2




    Usually it's an INNER JOIN that causes missing results in queries. It's hard for a LEFT JOIN to cause this issue.
    – Joel Coehoorn
    Nov 5 at 22:44










  • Yeah. It's a weird one. That's what pushed me to stack overflow. I've passed this to every other coder here and to my boss and everyone is scratching their heads. The database seems fine because we have a bunch of other queries calling these tables that work just fine. It's just one small subset of queries of this type, which don't seem to have anything wrong with them, that are having issues. Been banging my head against the wall all day.
    – Jeremiah
    Nov 5 at 22:50











  • What versions of MySQL, client & server? See Minimal, Complete, and Verifiable example.
    – philipxy
    Nov 11 at 19:15















I see nothing wrong with the left outer join in your query. I'd prefer an IN or EXISTS clause in the select clause, but the left outer join should work just as fine. Maybe there is something wrong with the database? Try REPAIR TABLE or mysqlcheck --repair (dev.mysql.com/doc/refman/5.7/en/…).
– Thorsten Kettner
Nov 5 at 22:33





I see nothing wrong with the left outer join in your query. I'd prefer an IN or EXISTS clause in the select clause, but the left outer join should work just as fine. Maybe there is something wrong with the database? Try REPAIR TABLE or mysqlcheck --repair (dev.mysql.com/doc/refman/5.7/en/…).
– Thorsten Kettner
Nov 5 at 22:33





2




2




Usually it's an INNER JOIN that causes missing results in queries. It's hard for a LEFT JOIN to cause this issue.
– Joel Coehoorn
Nov 5 at 22:44




Usually it's an INNER JOIN that causes missing results in queries. It's hard for a LEFT JOIN to cause this issue.
– Joel Coehoorn
Nov 5 at 22:44












Yeah. It's a weird one. That's what pushed me to stack overflow. I've passed this to every other coder here and to my boss and everyone is scratching their heads. The database seems fine because we have a bunch of other queries calling these tables that work just fine. It's just one small subset of queries of this type, which don't seem to have anything wrong with them, that are having issues. Been banging my head against the wall all day.
– Jeremiah
Nov 5 at 22:50





Yeah. It's a weird one. That's what pushed me to stack overflow. I've passed this to every other coder here and to my boss and everyone is scratching their heads. The database seems fine because we have a bunch of other queries calling these tables that work just fine. It's just one small subset of queries of this type, which don't seem to have anything wrong with them, that are having issues. Been banging my head against the wall all day.
– Jeremiah
Nov 5 at 22:50













What versions of MySQL, client & server? See Minimal, Complete, and Verifiable example.
– philipxy
Nov 11 at 19:15




What versions of MySQL, client & server? See Minimal, Complete, and Verifiable example.
– philipxy
Nov 11 at 19:15












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










This sounds like a MySQL bug. As a workaround, you can use EXISTS with a correlated subquery.



SELECT
DATE(BT.DateCheckedIn) AS X,
DAYOFWEEK(BT.DateCheckedIn) AS DayX,
SUM(IR.QtyCheckedIn) AS C,
AU.AdminUsername,
AU.AdminFirstName,
AU.AdminLastName,
IF(EXISTS(
SELECT 1
FROM log_production_bt BTE
WHERE
DATE(BT.DateCheckedIn) = DATE(BTE.ProductionDate)
AND BTE.ProductionSection = 'wtransactions'
AND AU.AdminUsername = BTE.ProductionUsername), 'NO', 'YES') AS Exclude
FROM
buying_issuesreceived IR
JOIN buying_transactions BT ON IR.TransactionID = BT.TransactionID
JOIN adminusers AU ON BT.CheckedInByAdminUserID = AU.AdminUserID
WHERE
DATE(BT.DateCheckedIn) BETWEEN DATE '2018-09-24' AND DATE '2018-09-30'
GROUP BY
DATE(BT.DateCheckedIn),
AU.AdminUsername





share|improve this answer




















  • This worked! Thanks!
    – Jeremiah
    Nov 5 at 23:31










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53163112%2fsql-is-intermittently-returning-no-data-in-response-to-a-left-join%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










This sounds like a MySQL bug. As a workaround, you can use EXISTS with a correlated subquery.



SELECT
DATE(BT.DateCheckedIn) AS X,
DAYOFWEEK(BT.DateCheckedIn) AS DayX,
SUM(IR.QtyCheckedIn) AS C,
AU.AdminUsername,
AU.AdminFirstName,
AU.AdminLastName,
IF(EXISTS(
SELECT 1
FROM log_production_bt BTE
WHERE
DATE(BT.DateCheckedIn) = DATE(BTE.ProductionDate)
AND BTE.ProductionSection = 'wtransactions'
AND AU.AdminUsername = BTE.ProductionUsername), 'NO', 'YES') AS Exclude
FROM
buying_issuesreceived IR
JOIN buying_transactions BT ON IR.TransactionID = BT.TransactionID
JOIN adminusers AU ON BT.CheckedInByAdminUserID = AU.AdminUserID
WHERE
DATE(BT.DateCheckedIn) BETWEEN DATE '2018-09-24' AND DATE '2018-09-30'
GROUP BY
DATE(BT.DateCheckedIn),
AU.AdminUsername





share|improve this answer




















  • This worked! Thanks!
    – Jeremiah
    Nov 5 at 23:31














up vote
0
down vote



accepted










This sounds like a MySQL bug. As a workaround, you can use EXISTS with a correlated subquery.



SELECT
DATE(BT.DateCheckedIn) AS X,
DAYOFWEEK(BT.DateCheckedIn) AS DayX,
SUM(IR.QtyCheckedIn) AS C,
AU.AdminUsername,
AU.AdminFirstName,
AU.AdminLastName,
IF(EXISTS(
SELECT 1
FROM log_production_bt BTE
WHERE
DATE(BT.DateCheckedIn) = DATE(BTE.ProductionDate)
AND BTE.ProductionSection = 'wtransactions'
AND AU.AdminUsername = BTE.ProductionUsername), 'NO', 'YES') AS Exclude
FROM
buying_issuesreceived IR
JOIN buying_transactions BT ON IR.TransactionID = BT.TransactionID
JOIN adminusers AU ON BT.CheckedInByAdminUserID = AU.AdminUserID
WHERE
DATE(BT.DateCheckedIn) BETWEEN DATE '2018-09-24' AND DATE '2018-09-30'
GROUP BY
DATE(BT.DateCheckedIn),
AU.AdminUsername





share|improve this answer




















  • This worked! Thanks!
    – Jeremiah
    Nov 5 at 23:31












up vote
0
down vote



accepted







up vote
0
down vote



accepted






This sounds like a MySQL bug. As a workaround, you can use EXISTS with a correlated subquery.



SELECT
DATE(BT.DateCheckedIn) AS X,
DAYOFWEEK(BT.DateCheckedIn) AS DayX,
SUM(IR.QtyCheckedIn) AS C,
AU.AdminUsername,
AU.AdminFirstName,
AU.AdminLastName,
IF(EXISTS(
SELECT 1
FROM log_production_bt BTE
WHERE
DATE(BT.DateCheckedIn) = DATE(BTE.ProductionDate)
AND BTE.ProductionSection = 'wtransactions'
AND AU.AdminUsername = BTE.ProductionUsername), 'NO', 'YES') AS Exclude
FROM
buying_issuesreceived IR
JOIN buying_transactions BT ON IR.TransactionID = BT.TransactionID
JOIN adminusers AU ON BT.CheckedInByAdminUserID = AU.AdminUserID
WHERE
DATE(BT.DateCheckedIn) BETWEEN DATE '2018-09-24' AND DATE '2018-09-30'
GROUP BY
DATE(BT.DateCheckedIn),
AU.AdminUsername





share|improve this answer












This sounds like a MySQL bug. As a workaround, you can use EXISTS with a correlated subquery.



SELECT
DATE(BT.DateCheckedIn) AS X,
DAYOFWEEK(BT.DateCheckedIn) AS DayX,
SUM(IR.QtyCheckedIn) AS C,
AU.AdminUsername,
AU.AdminFirstName,
AU.AdminLastName,
IF(EXISTS(
SELECT 1
FROM log_production_bt BTE
WHERE
DATE(BT.DateCheckedIn) = DATE(BTE.ProductionDate)
AND BTE.ProductionSection = 'wtransactions'
AND AU.AdminUsername = BTE.ProductionUsername), 'NO', 'YES') AS Exclude
FROM
buying_issuesreceived IR
JOIN buying_transactions BT ON IR.TransactionID = BT.TransactionID
JOIN adminusers AU ON BT.CheckedInByAdminUserID = AU.AdminUserID
WHERE
DATE(BT.DateCheckedIn) BETWEEN DATE '2018-09-24' AND DATE '2018-09-30'
GROUP BY
DATE(BT.DateCheckedIn),
AU.AdminUsername






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 5 at 23:01









Barmar

414k34239340




414k34239340











  • This worked! Thanks!
    – Jeremiah
    Nov 5 at 23:31
















  • This worked! Thanks!
    – Jeremiah
    Nov 5 at 23:31















This worked! Thanks!
– Jeremiah
Nov 5 at 23:31




This worked! Thanks!
– Jeremiah
Nov 5 at 23:31

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53163112%2fsql-is-intermittently-returning-no-data-in-response-to-a-left-join%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto