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.
mysql sql database join left-join
add a comment |
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.
mysql sql database join left-join
I see nothing wrong with the left outer join in your query. I'd prefer anIN
orEXISTS
clause in the select clause, but the left outer join should work just as fine. Maybe there is something wrong with the database? TryREPAIR TABLE
ormysqlcheck --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
add a comment |
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.
mysql sql database join left-join
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
mysql sql database join left-join
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 anIN
orEXISTS
clause in the select clause, but the left outer join should work just as fine. Maybe there is something wrong with the database? TryREPAIR TABLE
ormysqlcheck --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
add a comment |
I see nothing wrong with the left outer join in your query. I'd prefer anIN
orEXISTS
clause in the select clause, but the left outer join should work just as fine. Maybe there is something wrong with the database? TryREPAIR TABLE
ormysqlcheck --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
add a comment |
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
This worked! Thanks!
– Jeremiah
Nov 5 at 23:31
add a comment |
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
This worked! Thanks!
– Jeremiah
Nov 5 at 23:31
add a comment |
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
This worked! Thanks!
– Jeremiah
Nov 5 at 23:31
add a comment |
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
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
answered Nov 5 at 23:01
Barmar
414k34239340
414k34239340
This worked! Thanks!
– Jeremiah
Nov 5 at 23:31
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
I see nothing wrong with the left outer join in your query. I'd prefer an
IN
orEXISTS
clause in the select clause, but the left outer join should work just as fine. Maybe there is something wrong with the database? TryREPAIR TABLE
ormysqlcheck --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