LEFT JOIN and IS NULL in nested CONCAT returning NULL only
Each student has multiple time period to pay fee, I want to fetch fee time period in which fee hasn't payed, or in MySQL
language - fetch row which is not in another table.
Here, I am using nested GROUP_CONCAT
in MySQL
, and LEFT JOIN
with IS NULL
Table student
- lists of student
id | ttl | cls | sec
===============================
1 | Rohit | 1 | 1
2 | Karuna | 2 | 0
Table cls
- lists of Class
id | ttl
===========
1 | One
2 | Two
Table sec
- lists of section
id | ttl
===========
1 | A
2 | B
Table fee_tm
- lists of Fee time Period
id | ttl
===========
1 | Jan
2 | Feb
Table std_fee
- lists of Fee period assigned to Student
id | s_id| f_id| fee| f_tm
====================================
1 | 1 | 4 | 100| 1
According to tables structure and row in table, I am expecting following output with my MySQL code.
//(student.id-student.cls-student.sec-student rest of the fee.time(Month1,Month2..))
1-Rohit-One-A-Feb,
2-Karuna-Two-John,Feb
but what I get (I wanna apply NULL
and LEFT JOIN
only for fee time, so remaining fee time can be fetched, but here it is apply to whole result)
2-Karuna-Two-
SQL Fiddle
MySQL Code
SELECT
GROUP_CONCAT(DISTINCT CONCAT(student.id,'-',student.ttl,'-',cls.ttl,'-',
COALESCE(sec.ttl,''),
COALESCE(CONCAT('-',fee_tm.ttl),''))
ORDER BY student.id) AS stdt
FROM
student
JOIN
cls ON cls.id=student.cls
LEFT JOIN
sec ON sec.id=student.sec
LEFT JOIN
std_fee ON std_fee.s_id = student.id
LEFT JOIN
fee_tm ON fee_tm.id = std_fee.f_tm
WHERE
std_fee.f_tm IS NUll
mysql sql group-by left-join group-concat
add a comment |
Each student has multiple time period to pay fee, I want to fetch fee time period in which fee hasn't payed, or in MySQL
language - fetch row which is not in another table.
Here, I am using nested GROUP_CONCAT
in MySQL
, and LEFT JOIN
with IS NULL
Table student
- lists of student
id | ttl | cls | sec
===============================
1 | Rohit | 1 | 1
2 | Karuna | 2 | 0
Table cls
- lists of Class
id | ttl
===========
1 | One
2 | Two
Table sec
- lists of section
id | ttl
===========
1 | A
2 | B
Table fee_tm
- lists of Fee time Period
id | ttl
===========
1 | Jan
2 | Feb
Table std_fee
- lists of Fee period assigned to Student
id | s_id| f_id| fee| f_tm
====================================
1 | 1 | 4 | 100| 1
According to tables structure and row in table, I am expecting following output with my MySQL code.
//(student.id-student.cls-student.sec-student rest of the fee.time(Month1,Month2..))
1-Rohit-One-A-Feb,
2-Karuna-Two-John,Feb
but what I get (I wanna apply NULL
and LEFT JOIN
only for fee time, so remaining fee time can be fetched, but here it is apply to whole result)
2-Karuna-Two-
SQL Fiddle
MySQL Code
SELECT
GROUP_CONCAT(DISTINCT CONCAT(student.id,'-',student.ttl,'-',cls.ttl,'-',
COALESCE(sec.ttl,''),
COALESCE(CONCAT('-',fee_tm.ttl),''))
ORDER BY student.id) AS stdt
FROM
student
JOIN
cls ON cls.id=student.cls
LEFT JOIN
sec ON sec.id=student.sec
LEFT JOIN
std_fee ON std_fee.s_id = student.id
LEFT JOIN
fee_tm ON fee_tm.id = std_fee.f_tm
WHERE
std_fee.f_tm IS NUll
mysql sql group-by left-join group-concat
1
Try movingWHERE std_fee.f_tm IS NULL
to theON
clause.
– Tim Biegeleisen
Nov 12 at 1:54
add a comment |
Each student has multiple time period to pay fee, I want to fetch fee time period in which fee hasn't payed, or in MySQL
language - fetch row which is not in another table.
Here, I am using nested GROUP_CONCAT
in MySQL
, and LEFT JOIN
with IS NULL
Table student
- lists of student
id | ttl | cls | sec
===============================
1 | Rohit | 1 | 1
2 | Karuna | 2 | 0
Table cls
- lists of Class
id | ttl
===========
1 | One
2 | Two
Table sec
- lists of section
id | ttl
===========
1 | A
2 | B
Table fee_tm
- lists of Fee time Period
id | ttl
===========
1 | Jan
2 | Feb
Table std_fee
- lists of Fee period assigned to Student
id | s_id| f_id| fee| f_tm
====================================
1 | 1 | 4 | 100| 1
According to tables structure and row in table, I am expecting following output with my MySQL code.
//(student.id-student.cls-student.sec-student rest of the fee.time(Month1,Month2..))
1-Rohit-One-A-Feb,
2-Karuna-Two-John,Feb
but what I get (I wanna apply NULL
and LEFT JOIN
only for fee time, so remaining fee time can be fetched, but here it is apply to whole result)
2-Karuna-Two-
SQL Fiddle
MySQL Code
SELECT
GROUP_CONCAT(DISTINCT CONCAT(student.id,'-',student.ttl,'-',cls.ttl,'-',
COALESCE(sec.ttl,''),
COALESCE(CONCAT('-',fee_tm.ttl),''))
ORDER BY student.id) AS stdt
FROM
student
JOIN
cls ON cls.id=student.cls
LEFT JOIN
sec ON sec.id=student.sec
LEFT JOIN
std_fee ON std_fee.s_id = student.id
LEFT JOIN
fee_tm ON fee_tm.id = std_fee.f_tm
WHERE
std_fee.f_tm IS NUll
mysql sql group-by left-join group-concat
Each student has multiple time period to pay fee, I want to fetch fee time period in which fee hasn't payed, or in MySQL
language - fetch row which is not in another table.
Here, I am using nested GROUP_CONCAT
in MySQL
, and LEFT JOIN
with IS NULL
Table student
- lists of student
id | ttl | cls | sec
===============================
1 | Rohit | 1 | 1
2 | Karuna | 2 | 0
Table cls
- lists of Class
id | ttl
===========
1 | One
2 | Two
Table sec
- lists of section
id | ttl
===========
1 | A
2 | B
Table fee_tm
- lists of Fee time Period
id | ttl
===========
1 | Jan
2 | Feb
Table std_fee
- lists of Fee period assigned to Student
id | s_id| f_id| fee| f_tm
====================================
1 | 1 | 4 | 100| 1
According to tables structure and row in table, I am expecting following output with my MySQL code.
//(student.id-student.cls-student.sec-student rest of the fee.time(Month1,Month2..))
1-Rohit-One-A-Feb,
2-Karuna-Two-John,Feb
but what I get (I wanna apply NULL
and LEFT JOIN
only for fee time, so remaining fee time can be fetched, but here it is apply to whole result)
2-Karuna-Two-
SQL Fiddle
MySQL Code
SELECT
GROUP_CONCAT(DISTINCT CONCAT(student.id,'-',student.ttl,'-',cls.ttl,'-',
COALESCE(sec.ttl,''),
COALESCE(CONCAT('-',fee_tm.ttl),''))
ORDER BY student.id) AS stdt
FROM
student
JOIN
cls ON cls.id=student.cls
LEFT JOIN
sec ON sec.id=student.sec
LEFT JOIN
std_fee ON std_fee.s_id = student.id
LEFT JOIN
fee_tm ON fee_tm.id = std_fee.f_tm
WHERE
std_fee.f_tm IS NUll
mysql sql group-by left-join group-concat
mysql sql group-by left-join group-concat
edited Nov 12 at 2:28
D-Shih
25.4k61531
25.4k61531
asked Nov 12 at 1:48
Dipak
498415
498415
1
Try movingWHERE std_fee.f_tm IS NULL
to theON
clause.
– Tim Biegeleisen
Nov 12 at 1:54
add a comment |
1
Try movingWHERE std_fee.f_tm IS NULL
to theON
clause.
– Tim Biegeleisen
Nov 12 at 1:54
1
1
Try moving
WHERE std_fee.f_tm IS NULL
to the ON
clause.– Tim Biegeleisen
Nov 12 at 1:54
Try moving
WHERE std_fee.f_tm IS NULL
to the ON
clause.– Tim Biegeleisen
Nov 12 at 1:54
add a comment |
1 Answer
1
active
oldest
votes
You can try to write a subquery for std_fee
and fee_tm
tables and let std_fee.f_tm IS NUll
condition in ON
to make a result set.
What's the difference let the condition between putting in
where
andON
?
You are using OUTER JOIN
if you don't put conditions in ON
you will miss row data by this std_fee.f_tm IS NUll
condition, because you match in fee_tm.id = std_fee.f_tm
query looks like this.
Query 1:
SELECT
GROUP_CONCAT(DISTINCT CONCAT(student.id,'-',student.ttl,'-',cls.ttl,'-',
COALESCE(sec.ttl,''),
COALESCE(CONCAT(t1.ttl),''))
ORDER BY student.id) AS stdt
FROM
student
JOIN
cls ON cls.id=student.cls
LEFT JOIN
sec ON sec.id=student.sec
LEFT JOIN
(
select s.id,GROUP_CONCAT(COALESCE(fee_tm.ttl,'')) ttl
FROM
student s
LEFT JOIN
std_fee ON std_fee.s_id = s.id
LEFT JOIN
fee_tm ON fee_tm.id = std_fee.f_tm or std_fee.f_tm IS NUll
GROUP BY s.id
) t1 on t1.id = student.id
group by student.id
Results:
| stdt |
|----------------------|
| 1-Rohit-One-AJan |
| 2-Karuna-Two-Jan,Feb |
Thanks but I think you don't understand my expected output. Addingstd_fee.f_tm IS NUll
inON
clause, I get following result -1-Rohit-One-A,2-Karuna-Two-
, Here all fee month are missing, I need1-Rohit-One-A-Feb,2-Karuna-Two-Jan, Feb
. So I tried replacingOR
instead ofAND
in your answer. And I am close to my expectation.1-Rohit-One-Jan,2-Karuna-Two-Feb,2-Karuna-Two-Jan
but still can't get exact result. link
– Dipak
Nov 12 at 2:22
@Dipak You need to use agroup by
clause for student id
– D-Shih
Nov 12 at 2:23
@Dipak No problem glad to help :)
– D-Shih
Nov 12 at 2:29
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f53255062%2fleft-join-and-is-null-in-nested-concat-returning-null-only%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
You can try to write a subquery for std_fee
and fee_tm
tables and let std_fee.f_tm IS NUll
condition in ON
to make a result set.
What's the difference let the condition between putting in
where
andON
?
You are using OUTER JOIN
if you don't put conditions in ON
you will miss row data by this std_fee.f_tm IS NUll
condition, because you match in fee_tm.id = std_fee.f_tm
query looks like this.
Query 1:
SELECT
GROUP_CONCAT(DISTINCT CONCAT(student.id,'-',student.ttl,'-',cls.ttl,'-',
COALESCE(sec.ttl,''),
COALESCE(CONCAT(t1.ttl),''))
ORDER BY student.id) AS stdt
FROM
student
JOIN
cls ON cls.id=student.cls
LEFT JOIN
sec ON sec.id=student.sec
LEFT JOIN
(
select s.id,GROUP_CONCAT(COALESCE(fee_tm.ttl,'')) ttl
FROM
student s
LEFT JOIN
std_fee ON std_fee.s_id = s.id
LEFT JOIN
fee_tm ON fee_tm.id = std_fee.f_tm or std_fee.f_tm IS NUll
GROUP BY s.id
) t1 on t1.id = student.id
group by student.id
Results:
| stdt |
|----------------------|
| 1-Rohit-One-AJan |
| 2-Karuna-Two-Jan,Feb |
Thanks but I think you don't understand my expected output. Addingstd_fee.f_tm IS NUll
inON
clause, I get following result -1-Rohit-One-A,2-Karuna-Two-
, Here all fee month are missing, I need1-Rohit-One-A-Feb,2-Karuna-Two-Jan, Feb
. So I tried replacingOR
instead ofAND
in your answer. And I am close to my expectation.1-Rohit-One-Jan,2-Karuna-Two-Feb,2-Karuna-Two-Jan
but still can't get exact result. link
– Dipak
Nov 12 at 2:22
@Dipak You need to use agroup by
clause for student id
– D-Shih
Nov 12 at 2:23
@Dipak No problem glad to help :)
– D-Shih
Nov 12 at 2:29
add a comment |
You can try to write a subquery for std_fee
and fee_tm
tables and let std_fee.f_tm IS NUll
condition in ON
to make a result set.
What's the difference let the condition between putting in
where
andON
?
You are using OUTER JOIN
if you don't put conditions in ON
you will miss row data by this std_fee.f_tm IS NUll
condition, because you match in fee_tm.id = std_fee.f_tm
query looks like this.
Query 1:
SELECT
GROUP_CONCAT(DISTINCT CONCAT(student.id,'-',student.ttl,'-',cls.ttl,'-',
COALESCE(sec.ttl,''),
COALESCE(CONCAT(t1.ttl),''))
ORDER BY student.id) AS stdt
FROM
student
JOIN
cls ON cls.id=student.cls
LEFT JOIN
sec ON sec.id=student.sec
LEFT JOIN
(
select s.id,GROUP_CONCAT(COALESCE(fee_tm.ttl,'')) ttl
FROM
student s
LEFT JOIN
std_fee ON std_fee.s_id = s.id
LEFT JOIN
fee_tm ON fee_tm.id = std_fee.f_tm or std_fee.f_tm IS NUll
GROUP BY s.id
) t1 on t1.id = student.id
group by student.id
Results:
| stdt |
|----------------------|
| 1-Rohit-One-AJan |
| 2-Karuna-Two-Jan,Feb |
Thanks but I think you don't understand my expected output. Addingstd_fee.f_tm IS NUll
inON
clause, I get following result -1-Rohit-One-A,2-Karuna-Two-
, Here all fee month are missing, I need1-Rohit-One-A-Feb,2-Karuna-Two-Jan, Feb
. So I tried replacingOR
instead ofAND
in your answer. And I am close to my expectation.1-Rohit-One-Jan,2-Karuna-Two-Feb,2-Karuna-Two-Jan
but still can't get exact result. link
– Dipak
Nov 12 at 2:22
@Dipak You need to use agroup by
clause for student id
– D-Shih
Nov 12 at 2:23
@Dipak No problem glad to help :)
– D-Shih
Nov 12 at 2:29
add a comment |
You can try to write a subquery for std_fee
and fee_tm
tables and let std_fee.f_tm IS NUll
condition in ON
to make a result set.
What's the difference let the condition between putting in
where
andON
?
You are using OUTER JOIN
if you don't put conditions in ON
you will miss row data by this std_fee.f_tm IS NUll
condition, because you match in fee_tm.id = std_fee.f_tm
query looks like this.
Query 1:
SELECT
GROUP_CONCAT(DISTINCT CONCAT(student.id,'-',student.ttl,'-',cls.ttl,'-',
COALESCE(sec.ttl,''),
COALESCE(CONCAT(t1.ttl),''))
ORDER BY student.id) AS stdt
FROM
student
JOIN
cls ON cls.id=student.cls
LEFT JOIN
sec ON sec.id=student.sec
LEFT JOIN
(
select s.id,GROUP_CONCAT(COALESCE(fee_tm.ttl,'')) ttl
FROM
student s
LEFT JOIN
std_fee ON std_fee.s_id = s.id
LEFT JOIN
fee_tm ON fee_tm.id = std_fee.f_tm or std_fee.f_tm IS NUll
GROUP BY s.id
) t1 on t1.id = student.id
group by student.id
Results:
| stdt |
|----------------------|
| 1-Rohit-One-AJan |
| 2-Karuna-Two-Jan,Feb |
You can try to write a subquery for std_fee
and fee_tm
tables and let std_fee.f_tm IS NUll
condition in ON
to make a result set.
What's the difference let the condition between putting in
where
andON
?
You are using OUTER JOIN
if you don't put conditions in ON
you will miss row data by this std_fee.f_tm IS NUll
condition, because you match in fee_tm.id = std_fee.f_tm
query looks like this.
Query 1:
SELECT
GROUP_CONCAT(DISTINCT CONCAT(student.id,'-',student.ttl,'-',cls.ttl,'-',
COALESCE(sec.ttl,''),
COALESCE(CONCAT(t1.ttl),''))
ORDER BY student.id) AS stdt
FROM
student
JOIN
cls ON cls.id=student.cls
LEFT JOIN
sec ON sec.id=student.sec
LEFT JOIN
(
select s.id,GROUP_CONCAT(COALESCE(fee_tm.ttl,'')) ttl
FROM
student s
LEFT JOIN
std_fee ON std_fee.s_id = s.id
LEFT JOIN
fee_tm ON fee_tm.id = std_fee.f_tm or std_fee.f_tm IS NUll
GROUP BY s.id
) t1 on t1.id = student.id
group by student.id
Results:
| stdt |
|----------------------|
| 1-Rohit-One-AJan |
| 2-Karuna-Two-Jan,Feb |
edited Nov 12 at 2:29
answered Nov 12 at 2:03
D-Shih
25.4k61531
25.4k61531
Thanks but I think you don't understand my expected output. Addingstd_fee.f_tm IS NUll
inON
clause, I get following result -1-Rohit-One-A,2-Karuna-Two-
, Here all fee month are missing, I need1-Rohit-One-A-Feb,2-Karuna-Two-Jan, Feb
. So I tried replacingOR
instead ofAND
in your answer. And I am close to my expectation.1-Rohit-One-Jan,2-Karuna-Two-Feb,2-Karuna-Two-Jan
but still can't get exact result. link
– Dipak
Nov 12 at 2:22
@Dipak You need to use agroup by
clause for student id
– D-Shih
Nov 12 at 2:23
@Dipak No problem glad to help :)
– D-Shih
Nov 12 at 2:29
add a comment |
Thanks but I think you don't understand my expected output. Addingstd_fee.f_tm IS NUll
inON
clause, I get following result -1-Rohit-One-A,2-Karuna-Two-
, Here all fee month are missing, I need1-Rohit-One-A-Feb,2-Karuna-Two-Jan, Feb
. So I tried replacingOR
instead ofAND
in your answer. And I am close to my expectation.1-Rohit-One-Jan,2-Karuna-Two-Feb,2-Karuna-Two-Jan
but still can't get exact result. link
– Dipak
Nov 12 at 2:22
@Dipak You need to use agroup by
clause for student id
– D-Shih
Nov 12 at 2:23
@Dipak No problem glad to help :)
– D-Shih
Nov 12 at 2:29
Thanks but I think you don't understand my expected output. Adding
std_fee.f_tm IS NUll
in ON
clause, I get following result - 1-Rohit-One-A,2-Karuna-Two-
, Here all fee month are missing, I need 1-Rohit-One-A-Feb,2-Karuna-Two-Jan, Feb
. So I tried replacing OR
instead of AND
in your answer. And I am close to my expectation. 1-Rohit-One-Jan,2-Karuna-Two-Feb,2-Karuna-Two-Jan
but still can't get exact result. link– Dipak
Nov 12 at 2:22
Thanks but I think you don't understand my expected output. Adding
std_fee.f_tm IS NUll
in ON
clause, I get following result - 1-Rohit-One-A,2-Karuna-Two-
, Here all fee month are missing, I need 1-Rohit-One-A-Feb,2-Karuna-Two-Jan, Feb
. So I tried replacing OR
instead of AND
in your answer. And I am close to my expectation. 1-Rohit-One-Jan,2-Karuna-Two-Feb,2-Karuna-Two-Jan
but still can't get exact result. link– Dipak
Nov 12 at 2:22
@Dipak You need to use a
group by
clause for student id– D-Shih
Nov 12 at 2:23
@Dipak You need to use a
group by
clause for student id– D-Shih
Nov 12 at 2:23
@Dipak No problem glad to help :)
– D-Shih
Nov 12 at 2:29
@Dipak No problem glad to help :)
– D-Shih
Nov 12 at 2:29
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%2f53255062%2fleft-join-and-is-null-in-nested-concat-returning-null-only%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
1
Try moving
WHERE std_fee.f_tm IS NULL
to theON
clause.– Tim Biegeleisen
Nov 12 at 1:54