JOIN 3 tables, 1 having many rows with same ID. I need to show all comments related to each ID












0















My JOIN below - 3 tables - Database structure straight forward . problem table #3 comment_id = primary AI, company_id indexed (used for join), other fields non indexed. Core problem = rows with same ID.



$stmt_E = $db2->prepare("
SELECT c.company_id
, c.company_name
, c.company_status
, c.effective_date
, c.ckby
, c.time
, e.ck1
, e.ckby1
, e.time1
, e.ck2
, e.ckby2
, e.time2
, e.ck3
, e.ckby3
, e.time3
, e.ck4
, e.ckby4
, e.time4
, e.ck5
, e.ckby5
, e.time5
, e.ck6
, e.ckby6
, e.time6
, e.ck7
, e.ckby7
, e.time7
, e.ck8
, e.ckby8
, e.time8
, e.ck9
, e.ckby9
, e.time9
, e.ck10
, e.ckby10
, e.time10
, cc.company_id
, cc.active_comment
, cc.active_comment_sentby
, cc.active_comment_time
FROM companies c
JOIN enrolled_checklist e
ON e.company_id = c.company_id
JOIN active_comments cc
ON cc.company_id = c.company_id
WHERE company_status = 'E'
ORDER
BY company_name asc
");
$stmt_E->bind_result($company_id, $company_name,$company_status,$effective_date,$ckby,$time,$eck1,$eckby1,$etime1,$eck2,$eckby2,$etime2,$eck3,$eckby3,$etime3,
$eck4,$eckby4,$etime4,$eck5,$eckby5,$etime5,$eck6,$eckby6,$etime6,$eck7,$eckby7,$etime7,$eck8,$eckby8,$etime8,
$eck9,$eckby9,$etime9,$eck10,$eckby10,$etime10,$cccompany_id,$active_comment,$active_comment_sentby,$active_comment_time );
$stmt_E->execute();
$stmt_E->store_result();


My Loop - echo HTML - THIS SECTION gets duplicated by the same number of comments in 3rd table rather than the actual comments populating in desired section



while ($stmt_E->fetch()){

echo"
$tr
<td width='25%'> etc...............


I use 2 table info for checklist --- works perfect



When I try to place comment, matched to corresponding company, problems begin.



<div class='col-xs-12 cl-sm-12 col-md-12 col-lg-12'>
<input type='textarea' name='active_comment' value='$active_comment -$cccompany_id' placeholder='' readonly>
</div>

";
}
?>


I simply need all Comments from 3rd table to populate without duplicating all other fields. there could be as many as 25 Comments to one company ID. I need them to display within each company's section.










share|improve this question




















  • 2





    This kind of problem is symptomatic of poor design. Any time you find yourself with enumerated columns (above,say, 2), alarm bells should start ringing

    – Strawberry
    Nov 17 '18 at 8:18
















0















My JOIN below - 3 tables - Database structure straight forward . problem table #3 comment_id = primary AI, company_id indexed (used for join), other fields non indexed. Core problem = rows with same ID.



$stmt_E = $db2->prepare("
SELECT c.company_id
, c.company_name
, c.company_status
, c.effective_date
, c.ckby
, c.time
, e.ck1
, e.ckby1
, e.time1
, e.ck2
, e.ckby2
, e.time2
, e.ck3
, e.ckby3
, e.time3
, e.ck4
, e.ckby4
, e.time4
, e.ck5
, e.ckby5
, e.time5
, e.ck6
, e.ckby6
, e.time6
, e.ck7
, e.ckby7
, e.time7
, e.ck8
, e.ckby8
, e.time8
, e.ck9
, e.ckby9
, e.time9
, e.ck10
, e.ckby10
, e.time10
, cc.company_id
, cc.active_comment
, cc.active_comment_sentby
, cc.active_comment_time
FROM companies c
JOIN enrolled_checklist e
ON e.company_id = c.company_id
JOIN active_comments cc
ON cc.company_id = c.company_id
WHERE company_status = 'E'
ORDER
BY company_name asc
");
$stmt_E->bind_result($company_id, $company_name,$company_status,$effective_date,$ckby,$time,$eck1,$eckby1,$etime1,$eck2,$eckby2,$etime2,$eck3,$eckby3,$etime3,
$eck4,$eckby4,$etime4,$eck5,$eckby5,$etime5,$eck6,$eckby6,$etime6,$eck7,$eckby7,$etime7,$eck8,$eckby8,$etime8,
$eck9,$eckby9,$etime9,$eck10,$eckby10,$etime10,$cccompany_id,$active_comment,$active_comment_sentby,$active_comment_time );
$stmt_E->execute();
$stmt_E->store_result();


My Loop - echo HTML - THIS SECTION gets duplicated by the same number of comments in 3rd table rather than the actual comments populating in desired section



while ($stmt_E->fetch()){

echo"
$tr
<td width='25%'> etc...............


I use 2 table info for checklist --- works perfect



When I try to place comment, matched to corresponding company, problems begin.



<div class='col-xs-12 cl-sm-12 col-md-12 col-lg-12'>
<input type='textarea' name='active_comment' value='$active_comment -$cccompany_id' placeholder='' readonly>
</div>

";
}
?>


I simply need all Comments from 3rd table to populate without duplicating all other fields. there could be as many as 25 Comments to one company ID. I need them to display within each company's section.










share|improve this question




















  • 2





    This kind of problem is symptomatic of poor design. Any time you find yourself with enumerated columns (above,say, 2), alarm bells should start ringing

    – Strawberry
    Nov 17 '18 at 8:18














0












0








0








My JOIN below - 3 tables - Database structure straight forward . problem table #3 comment_id = primary AI, company_id indexed (used for join), other fields non indexed. Core problem = rows with same ID.



$stmt_E = $db2->prepare("
SELECT c.company_id
, c.company_name
, c.company_status
, c.effective_date
, c.ckby
, c.time
, e.ck1
, e.ckby1
, e.time1
, e.ck2
, e.ckby2
, e.time2
, e.ck3
, e.ckby3
, e.time3
, e.ck4
, e.ckby4
, e.time4
, e.ck5
, e.ckby5
, e.time5
, e.ck6
, e.ckby6
, e.time6
, e.ck7
, e.ckby7
, e.time7
, e.ck8
, e.ckby8
, e.time8
, e.ck9
, e.ckby9
, e.time9
, e.ck10
, e.ckby10
, e.time10
, cc.company_id
, cc.active_comment
, cc.active_comment_sentby
, cc.active_comment_time
FROM companies c
JOIN enrolled_checklist e
ON e.company_id = c.company_id
JOIN active_comments cc
ON cc.company_id = c.company_id
WHERE company_status = 'E'
ORDER
BY company_name asc
");
$stmt_E->bind_result($company_id, $company_name,$company_status,$effective_date,$ckby,$time,$eck1,$eckby1,$etime1,$eck2,$eckby2,$etime2,$eck3,$eckby3,$etime3,
$eck4,$eckby4,$etime4,$eck5,$eckby5,$etime5,$eck6,$eckby6,$etime6,$eck7,$eckby7,$etime7,$eck8,$eckby8,$etime8,
$eck9,$eckby9,$etime9,$eck10,$eckby10,$etime10,$cccompany_id,$active_comment,$active_comment_sentby,$active_comment_time );
$stmt_E->execute();
$stmt_E->store_result();


My Loop - echo HTML - THIS SECTION gets duplicated by the same number of comments in 3rd table rather than the actual comments populating in desired section



while ($stmt_E->fetch()){

echo"
$tr
<td width='25%'> etc...............


I use 2 table info for checklist --- works perfect



When I try to place comment, matched to corresponding company, problems begin.



<div class='col-xs-12 cl-sm-12 col-md-12 col-lg-12'>
<input type='textarea' name='active_comment' value='$active_comment -$cccompany_id' placeholder='' readonly>
</div>

";
}
?>


I simply need all Comments from 3rd table to populate without duplicating all other fields. there could be as many as 25 Comments to one company ID. I need them to display within each company's section.










share|improve this question
















My JOIN below - 3 tables - Database structure straight forward . problem table #3 comment_id = primary AI, company_id indexed (used for join), other fields non indexed. Core problem = rows with same ID.



$stmt_E = $db2->prepare("
SELECT c.company_id
, c.company_name
, c.company_status
, c.effective_date
, c.ckby
, c.time
, e.ck1
, e.ckby1
, e.time1
, e.ck2
, e.ckby2
, e.time2
, e.ck3
, e.ckby3
, e.time3
, e.ck4
, e.ckby4
, e.time4
, e.ck5
, e.ckby5
, e.time5
, e.ck6
, e.ckby6
, e.time6
, e.ck7
, e.ckby7
, e.time7
, e.ck8
, e.ckby8
, e.time8
, e.ck9
, e.ckby9
, e.time9
, e.ck10
, e.ckby10
, e.time10
, cc.company_id
, cc.active_comment
, cc.active_comment_sentby
, cc.active_comment_time
FROM companies c
JOIN enrolled_checklist e
ON e.company_id = c.company_id
JOIN active_comments cc
ON cc.company_id = c.company_id
WHERE company_status = 'E'
ORDER
BY company_name asc
");
$stmt_E->bind_result($company_id, $company_name,$company_status,$effective_date,$ckby,$time,$eck1,$eckby1,$etime1,$eck2,$eckby2,$etime2,$eck3,$eckby3,$etime3,
$eck4,$eckby4,$etime4,$eck5,$eckby5,$etime5,$eck6,$eckby6,$etime6,$eck7,$eckby7,$etime7,$eck8,$eckby8,$etime8,
$eck9,$eckby9,$etime9,$eck10,$eckby10,$etime10,$cccompany_id,$active_comment,$active_comment_sentby,$active_comment_time );
$stmt_E->execute();
$stmt_E->store_result();


My Loop - echo HTML - THIS SECTION gets duplicated by the same number of comments in 3rd table rather than the actual comments populating in desired section



while ($stmt_E->fetch()){

echo"
$tr
<td width='25%'> etc...............


I use 2 table info for checklist --- works perfect



When I try to place comment, matched to corresponding company, problems begin.



<div class='col-xs-12 cl-sm-12 col-md-12 col-lg-12'>
<input type='textarea' name='active_comment' value='$active_comment -$cccompany_id' placeholder='' readonly>
</div>

";
}
?>


I simply need all Comments from 3rd table to populate without duplicating all other fields. there could be as many as 25 Comments to one company ID. I need them to display within each company's section.







php mysql loops join






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 11:07









Strawberry

25.9k83149




25.9k83149










asked Nov 17 '18 at 7:49









kmbrokerkmbroker

11




11








  • 2





    This kind of problem is symptomatic of poor design. Any time you find yourself with enumerated columns (above,say, 2), alarm bells should start ringing

    – Strawberry
    Nov 17 '18 at 8:18














  • 2





    This kind of problem is symptomatic of poor design. Any time you find yourself with enumerated columns (above,say, 2), alarm bells should start ringing

    – Strawberry
    Nov 17 '18 at 8:18








2




2





This kind of problem is symptomatic of poor design. Any time you find yourself with enumerated columns (above,say, 2), alarm bells should start ringing

– Strawberry
Nov 17 '18 at 8:18





This kind of problem is symptomatic of poor design. Any time you find yourself with enumerated columns (above,say, 2), alarm bells should start ringing

– Strawberry
Nov 17 '18 at 8:18












0






active

oldest

votes











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53349303%2fjoin-3-tables-1-having-many-rows-with-same-id-i-need-to-show-all-comments-rela%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53349303%2fjoin-3-tables-1-having-many-rows-with-same-id-i-need-to-show-all-comments-rela%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







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)