How to join three row of result from a join one to many to multiple columns











up vote
2
down vote

favorite












I have to get the three first results of a one to many join.
For example



JOIN `company`.`Reason`
ON ((`company`.`Reason`.`quality_id` = `company`.`Quality`.`id`)))


where Quality has a one to many relation to Reason.



And I need to show in a column REASON_1 the first result from Reason, in REASON_2 the second and REASON_3 the third.



At the moment, I show only one row of result this way :



  `company`.`Reason`.`status`                                                AS `Reason`


And I don't know how this works, how does it select one row, and which one ?

And how can I do the same, but only with 3 rows and with different column name ?



Quality



ID | a  | b
1 | a1 | b1
2 | a2 | b2


Reason



ID | quality_id  | status
1 | quality_id_1 | status_1
2 | quality_id_1 | status_2
2 | quality_id_2 | status_1
2 | quality_id_2 | status_3


So if I want to get Reasons of Quality of id 1 I would get



REASON_1 | REASON_2 | REASON_3
status_1 | status_2 | null









share|improve this question




















  • 1




    It might be better to include some more information about the structure of the data you are querying so we can help better.Have you tried doing a row number on the columns ordered by reason and returning the first three results?
    – auxoutin
    Nov 7 at 9:15










  • What decides which row ends up in Reason_1, Reason_2, Reason_3 - do you have some kind of ordering column?
    – Bridge
    Nov 7 at 9:16










  • @Bridge Nothing, I only need to put the column "status" of the table Reason in Reason_1, 2 and 3.
    – Dimitri Danilov
    Nov 7 at 9:19















up vote
2
down vote

favorite












I have to get the three first results of a one to many join.
For example



JOIN `company`.`Reason`
ON ((`company`.`Reason`.`quality_id` = `company`.`Quality`.`id`)))


where Quality has a one to many relation to Reason.



And I need to show in a column REASON_1 the first result from Reason, in REASON_2 the second and REASON_3 the third.



At the moment, I show only one row of result this way :



  `company`.`Reason`.`status`                                                AS `Reason`


And I don't know how this works, how does it select one row, and which one ?

And how can I do the same, but only with 3 rows and with different column name ?



Quality



ID | a  | b
1 | a1 | b1
2 | a2 | b2


Reason



ID | quality_id  | status
1 | quality_id_1 | status_1
2 | quality_id_1 | status_2
2 | quality_id_2 | status_1
2 | quality_id_2 | status_3


So if I want to get Reasons of Quality of id 1 I would get



REASON_1 | REASON_2 | REASON_3
status_1 | status_2 | null









share|improve this question




















  • 1




    It might be better to include some more information about the structure of the data you are querying so we can help better.Have you tried doing a row number on the columns ordered by reason and returning the first three results?
    – auxoutin
    Nov 7 at 9:15










  • What decides which row ends up in Reason_1, Reason_2, Reason_3 - do you have some kind of ordering column?
    – Bridge
    Nov 7 at 9:16










  • @Bridge Nothing, I only need to put the column "status" of the table Reason in Reason_1, 2 and 3.
    – Dimitri Danilov
    Nov 7 at 9:19













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have to get the three first results of a one to many join.
For example



JOIN `company`.`Reason`
ON ((`company`.`Reason`.`quality_id` = `company`.`Quality`.`id`)))


where Quality has a one to many relation to Reason.



And I need to show in a column REASON_1 the first result from Reason, in REASON_2 the second and REASON_3 the third.



At the moment, I show only one row of result this way :



  `company`.`Reason`.`status`                                                AS `Reason`


And I don't know how this works, how does it select one row, and which one ?

And how can I do the same, but only with 3 rows and with different column name ?



Quality



ID | a  | b
1 | a1 | b1
2 | a2 | b2


Reason



ID | quality_id  | status
1 | quality_id_1 | status_1
2 | quality_id_1 | status_2
2 | quality_id_2 | status_1
2 | quality_id_2 | status_3


So if I want to get Reasons of Quality of id 1 I would get



REASON_1 | REASON_2 | REASON_3
status_1 | status_2 | null









share|improve this question















I have to get the three first results of a one to many join.
For example



JOIN `company`.`Reason`
ON ((`company`.`Reason`.`quality_id` = `company`.`Quality`.`id`)))


where Quality has a one to many relation to Reason.



And I need to show in a column REASON_1 the first result from Reason, in REASON_2 the second and REASON_3 the third.



At the moment, I show only one row of result this way :



  `company`.`Reason`.`status`                                                AS `Reason`


And I don't know how this works, how does it select one row, and which one ?

And how can I do the same, but only with 3 rows and with different column name ?



Quality



ID | a  | b
1 | a1 | b1
2 | a2 | b2


Reason



ID | quality_id  | status
1 | quality_id_1 | status_1
2 | quality_id_1 | status_2
2 | quality_id_2 | status_1
2 | quality_id_2 | status_3


So if I want to get Reasons of Quality of id 1 I would get



REASON_1 | REASON_2 | REASON_3
status_1 | status_2 | null






mysql sql sql-view






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 9:45

























asked Nov 7 at 9:12









Dimitri Danilov

6931926




6931926








  • 1




    It might be better to include some more information about the structure of the data you are querying so we can help better.Have you tried doing a row number on the columns ordered by reason and returning the first three results?
    – auxoutin
    Nov 7 at 9:15










  • What decides which row ends up in Reason_1, Reason_2, Reason_3 - do you have some kind of ordering column?
    – Bridge
    Nov 7 at 9:16










  • @Bridge Nothing, I only need to put the column "status" of the table Reason in Reason_1, 2 and 3.
    – Dimitri Danilov
    Nov 7 at 9:19














  • 1




    It might be better to include some more information about the structure of the data you are querying so we can help better.Have you tried doing a row number on the columns ordered by reason and returning the first three results?
    – auxoutin
    Nov 7 at 9:15










  • What decides which row ends up in Reason_1, Reason_2, Reason_3 - do you have some kind of ordering column?
    – Bridge
    Nov 7 at 9:16










  • @Bridge Nothing, I only need to put the column "status" of the table Reason in Reason_1, 2 and 3.
    – Dimitri Danilov
    Nov 7 at 9:19








1




1




It might be better to include some more information about the structure of the data you are querying so we can help better.Have you tried doing a row number on the columns ordered by reason and returning the first three results?
– auxoutin
Nov 7 at 9:15




It might be better to include some more information about the structure of the data you are querying so we can help better.Have you tried doing a row number on the columns ordered by reason and returning the first three results?
– auxoutin
Nov 7 at 9:15












What decides which row ends up in Reason_1, Reason_2, Reason_3 - do you have some kind of ordering column?
– Bridge
Nov 7 at 9:16




What decides which row ends up in Reason_1, Reason_2, Reason_3 - do you have some kind of ordering column?
– Bridge
Nov 7 at 9:16












@Bridge Nothing, I only need to put the column "status" of the table Reason in Reason_1, 2 and 3.
– Dimitri Danilov
Nov 7 at 9:19




@Bridge Nothing, I only need to put the column "status" of the table Reason in Reason_1, 2 and 3.
– Dimitri Danilov
Nov 7 at 9:19












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










You need to create an ordering within the reason table partitioned by the id from quality and then pivot this out on the columns from quality that you wish to return. In my code below i have done this using variables and a subquery in order to avoid having to making modifications to the base tables.



CREATE TABLE Quality (
QualityID INT AUTO_INCREMENT,
Value VARCHAR(50),
PRIMARY KEY(QualityID)
);

CREATE TABLE Reason (
ReasonID INT AUTO_INCREMENT,
QualityID INT,
Status varchar(50),
PRIMARY KEY(ReasonID)
);

INSERT INTO Quality (Value) VALUES ('A1'),('A2');

INSERT INTO Reason (QualityID, Status) VALUES (1,'Test1'),(1,'Test2'),(2,'Test3'),(2,'Test4'),(2,'Test5');

SET @RC = 0;
SET @QC = 0;

SELECT
Q.Value,
MAX(IF(R.OrderNo = 1, R.Status, NULL)) AS Status1,
MAX(IF(R.OrderNo = 2, R.Status, NULL)) AS Status2,
MAX(IF(R.OrderNo = 3, R.Status, NULL)) AS Status3
FROM Quality Q
INNER JOIN (SELECT
@RC:=CASE
WHEN @QC = QualityID THEN @RC + 1
ELSE 1
END AS OrderNo,
@QC:=QualityID as QualityID,
Status
FROM Reason R)R ON R.QualityID = Q.QualityID
GROUP BY Q.Value;





share|improve this answer























  • Thanks for your complete answer, but I don't use SQL Server, I use mysql, and I don't think there is "pivot" here :/
    – Dimitri Danilov
    Nov 7 at 9:59










  • Your solution will need to be a lot less elegant then can you store an ordering for the Status in your reason table?
    – auxoutin
    Nov 7 at 10:32










  • Unfortunatly I can't, I don't have the permission de modify them.
    – Dimitri Danilov
    Nov 7 at 10:45










  • I have modified the code so it should now work in mysql but the principles are the same. Hopefully this will work for you.
    – auxoutin
    Nov 7 at 10:59










  • Thank you so much to try helping me ! I tried to adapt it to my database stucture but I got the error that I cannot use variables within a sql view. Sorry for ommiting it in my post, I just putted a tag.
    – Dimitri Danilov
    Nov 7 at 11:19











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%2f53186411%2fhow-to-join-three-row-of-result-from-a-join-one-to-many-to-multiple-columns%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
2
down vote



accepted










You need to create an ordering within the reason table partitioned by the id from quality and then pivot this out on the columns from quality that you wish to return. In my code below i have done this using variables and a subquery in order to avoid having to making modifications to the base tables.



CREATE TABLE Quality (
QualityID INT AUTO_INCREMENT,
Value VARCHAR(50),
PRIMARY KEY(QualityID)
);

CREATE TABLE Reason (
ReasonID INT AUTO_INCREMENT,
QualityID INT,
Status varchar(50),
PRIMARY KEY(ReasonID)
);

INSERT INTO Quality (Value) VALUES ('A1'),('A2');

INSERT INTO Reason (QualityID, Status) VALUES (1,'Test1'),(1,'Test2'),(2,'Test3'),(2,'Test4'),(2,'Test5');

SET @RC = 0;
SET @QC = 0;

SELECT
Q.Value,
MAX(IF(R.OrderNo = 1, R.Status, NULL)) AS Status1,
MAX(IF(R.OrderNo = 2, R.Status, NULL)) AS Status2,
MAX(IF(R.OrderNo = 3, R.Status, NULL)) AS Status3
FROM Quality Q
INNER JOIN (SELECT
@RC:=CASE
WHEN @QC = QualityID THEN @RC + 1
ELSE 1
END AS OrderNo,
@QC:=QualityID as QualityID,
Status
FROM Reason R)R ON R.QualityID = Q.QualityID
GROUP BY Q.Value;





share|improve this answer























  • Thanks for your complete answer, but I don't use SQL Server, I use mysql, and I don't think there is "pivot" here :/
    – Dimitri Danilov
    Nov 7 at 9:59










  • Your solution will need to be a lot less elegant then can you store an ordering for the Status in your reason table?
    – auxoutin
    Nov 7 at 10:32










  • Unfortunatly I can't, I don't have the permission de modify them.
    – Dimitri Danilov
    Nov 7 at 10:45










  • I have modified the code so it should now work in mysql but the principles are the same. Hopefully this will work for you.
    – auxoutin
    Nov 7 at 10:59










  • Thank you so much to try helping me ! I tried to adapt it to my database stucture but I got the error that I cannot use variables within a sql view. Sorry for ommiting it in my post, I just putted a tag.
    – Dimitri Danilov
    Nov 7 at 11:19















up vote
2
down vote



accepted










You need to create an ordering within the reason table partitioned by the id from quality and then pivot this out on the columns from quality that you wish to return. In my code below i have done this using variables and a subquery in order to avoid having to making modifications to the base tables.



CREATE TABLE Quality (
QualityID INT AUTO_INCREMENT,
Value VARCHAR(50),
PRIMARY KEY(QualityID)
);

CREATE TABLE Reason (
ReasonID INT AUTO_INCREMENT,
QualityID INT,
Status varchar(50),
PRIMARY KEY(ReasonID)
);

INSERT INTO Quality (Value) VALUES ('A1'),('A2');

INSERT INTO Reason (QualityID, Status) VALUES (1,'Test1'),(1,'Test2'),(2,'Test3'),(2,'Test4'),(2,'Test5');

SET @RC = 0;
SET @QC = 0;

SELECT
Q.Value,
MAX(IF(R.OrderNo = 1, R.Status, NULL)) AS Status1,
MAX(IF(R.OrderNo = 2, R.Status, NULL)) AS Status2,
MAX(IF(R.OrderNo = 3, R.Status, NULL)) AS Status3
FROM Quality Q
INNER JOIN (SELECT
@RC:=CASE
WHEN @QC = QualityID THEN @RC + 1
ELSE 1
END AS OrderNo,
@QC:=QualityID as QualityID,
Status
FROM Reason R)R ON R.QualityID = Q.QualityID
GROUP BY Q.Value;





share|improve this answer























  • Thanks for your complete answer, but I don't use SQL Server, I use mysql, and I don't think there is "pivot" here :/
    – Dimitri Danilov
    Nov 7 at 9:59










  • Your solution will need to be a lot less elegant then can you store an ordering for the Status in your reason table?
    – auxoutin
    Nov 7 at 10:32










  • Unfortunatly I can't, I don't have the permission de modify them.
    – Dimitri Danilov
    Nov 7 at 10:45










  • I have modified the code so it should now work in mysql but the principles are the same. Hopefully this will work for you.
    – auxoutin
    Nov 7 at 10:59










  • Thank you so much to try helping me ! I tried to adapt it to my database stucture but I got the error that I cannot use variables within a sql view. Sorry for ommiting it in my post, I just putted a tag.
    – Dimitri Danilov
    Nov 7 at 11:19













up vote
2
down vote



accepted







up vote
2
down vote



accepted






You need to create an ordering within the reason table partitioned by the id from quality and then pivot this out on the columns from quality that you wish to return. In my code below i have done this using variables and a subquery in order to avoid having to making modifications to the base tables.



CREATE TABLE Quality (
QualityID INT AUTO_INCREMENT,
Value VARCHAR(50),
PRIMARY KEY(QualityID)
);

CREATE TABLE Reason (
ReasonID INT AUTO_INCREMENT,
QualityID INT,
Status varchar(50),
PRIMARY KEY(ReasonID)
);

INSERT INTO Quality (Value) VALUES ('A1'),('A2');

INSERT INTO Reason (QualityID, Status) VALUES (1,'Test1'),(1,'Test2'),(2,'Test3'),(2,'Test4'),(2,'Test5');

SET @RC = 0;
SET @QC = 0;

SELECT
Q.Value,
MAX(IF(R.OrderNo = 1, R.Status, NULL)) AS Status1,
MAX(IF(R.OrderNo = 2, R.Status, NULL)) AS Status2,
MAX(IF(R.OrderNo = 3, R.Status, NULL)) AS Status3
FROM Quality Q
INNER JOIN (SELECT
@RC:=CASE
WHEN @QC = QualityID THEN @RC + 1
ELSE 1
END AS OrderNo,
@QC:=QualityID as QualityID,
Status
FROM Reason R)R ON R.QualityID = Q.QualityID
GROUP BY Q.Value;





share|improve this answer














You need to create an ordering within the reason table partitioned by the id from quality and then pivot this out on the columns from quality that you wish to return. In my code below i have done this using variables and a subquery in order to avoid having to making modifications to the base tables.



CREATE TABLE Quality (
QualityID INT AUTO_INCREMENT,
Value VARCHAR(50),
PRIMARY KEY(QualityID)
);

CREATE TABLE Reason (
ReasonID INT AUTO_INCREMENT,
QualityID INT,
Status varchar(50),
PRIMARY KEY(ReasonID)
);

INSERT INTO Quality (Value) VALUES ('A1'),('A2');

INSERT INTO Reason (QualityID, Status) VALUES (1,'Test1'),(1,'Test2'),(2,'Test3'),(2,'Test4'),(2,'Test5');

SET @RC = 0;
SET @QC = 0;

SELECT
Q.Value,
MAX(IF(R.OrderNo = 1, R.Status, NULL)) AS Status1,
MAX(IF(R.OrderNo = 2, R.Status, NULL)) AS Status2,
MAX(IF(R.OrderNo = 3, R.Status, NULL)) AS Status3
FROM Quality Q
INNER JOIN (SELECT
@RC:=CASE
WHEN @QC = QualityID THEN @RC + 1
ELSE 1
END AS OrderNo,
@QC:=QualityID as QualityID,
Status
FROM Reason R)R ON R.QualityID = Q.QualityID
GROUP BY Q.Value;






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 7 at 10:58

























answered Nov 7 at 9:50









auxoutin

915




915












  • Thanks for your complete answer, but I don't use SQL Server, I use mysql, and I don't think there is "pivot" here :/
    – Dimitri Danilov
    Nov 7 at 9:59










  • Your solution will need to be a lot less elegant then can you store an ordering for the Status in your reason table?
    – auxoutin
    Nov 7 at 10:32










  • Unfortunatly I can't, I don't have the permission de modify them.
    – Dimitri Danilov
    Nov 7 at 10:45










  • I have modified the code so it should now work in mysql but the principles are the same. Hopefully this will work for you.
    – auxoutin
    Nov 7 at 10:59










  • Thank you so much to try helping me ! I tried to adapt it to my database stucture but I got the error that I cannot use variables within a sql view. Sorry for ommiting it in my post, I just putted a tag.
    – Dimitri Danilov
    Nov 7 at 11:19


















  • Thanks for your complete answer, but I don't use SQL Server, I use mysql, and I don't think there is "pivot" here :/
    – Dimitri Danilov
    Nov 7 at 9:59










  • Your solution will need to be a lot less elegant then can you store an ordering for the Status in your reason table?
    – auxoutin
    Nov 7 at 10:32










  • Unfortunatly I can't, I don't have the permission de modify them.
    – Dimitri Danilov
    Nov 7 at 10:45










  • I have modified the code so it should now work in mysql but the principles are the same. Hopefully this will work for you.
    – auxoutin
    Nov 7 at 10:59










  • Thank you so much to try helping me ! I tried to adapt it to my database stucture but I got the error that I cannot use variables within a sql view. Sorry for ommiting it in my post, I just putted a tag.
    – Dimitri Danilov
    Nov 7 at 11:19
















Thanks for your complete answer, but I don't use SQL Server, I use mysql, and I don't think there is "pivot" here :/
– Dimitri Danilov
Nov 7 at 9:59




Thanks for your complete answer, but I don't use SQL Server, I use mysql, and I don't think there is "pivot" here :/
– Dimitri Danilov
Nov 7 at 9:59












Your solution will need to be a lot less elegant then can you store an ordering for the Status in your reason table?
– auxoutin
Nov 7 at 10:32




Your solution will need to be a lot less elegant then can you store an ordering for the Status in your reason table?
– auxoutin
Nov 7 at 10:32












Unfortunatly I can't, I don't have the permission de modify them.
– Dimitri Danilov
Nov 7 at 10:45




Unfortunatly I can't, I don't have the permission de modify them.
– Dimitri Danilov
Nov 7 at 10:45












I have modified the code so it should now work in mysql but the principles are the same. Hopefully this will work for you.
– auxoutin
Nov 7 at 10:59




I have modified the code so it should now work in mysql but the principles are the same. Hopefully this will work for you.
– auxoutin
Nov 7 at 10:59












Thank you so much to try helping me ! I tried to adapt it to my database stucture but I got the error that I cannot use variables within a sql view. Sorry for ommiting it in my post, I just putted a tag.
– Dimitri Danilov
Nov 7 at 11:19




Thank you so much to try helping me ! I tried to adapt it to my database stucture but I got the error that I cannot use variables within a sql view. Sorry for ommiting it in my post, I just putted a tag.
– Dimitri Danilov
Nov 7 at 11:19


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186411%2fhow-to-join-three-row-of-result-from-a-join-one-to-many-to-multiple-columns%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







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()