mysqli: merge rows in 1 based on some parameters
i have a mysql table and there i have same text but in different language
ID, Lang, Text, number, chapter
1, English, Hello, 2, 1
4, German, Hallo, 2, 1
5, Spanish, Hola, 2, 1
so how can see those like this
Select * from table where chapter=1 (and language: english,german,spanish)
ID, lang1, lang2, lang3, number, chapter
5, Hello, Hallo, Hola, 2, 1
i want to join columns in 1 row
mysql sql
add a comment |
i have a mysql table and there i have same text but in different language
ID, Lang, Text, number, chapter
1, English, Hello, 2, 1
4, German, Hallo, 2, 1
5, Spanish, Hola, 2, 1
so how can see those like this
Select * from table where chapter=1 (and language: english,german,spanish)
ID, lang1, lang2, lang3, number, chapter
5, Hello, Hallo, Hola, 2, 1
i want to join columns in 1 row
mysql sql
1
Possible duplicate of MySQL pivot table
– Madhur Bhaiya
Nov 14 '18 at 11:05
Consider handling data display related requirements in your application code (eg: PHP, C++. Java etc)
– Madhur Bhaiya
Nov 14 '18 at 11:05
2
If you don't mind to get onelang
column with a comma separted string you can also consider GROUP_CONCAT (dev.mysql.com/doc/refman/8.0/en/…) instead.. Besides it's more eazy because the pivot query needs to be created dynamic to support more columns.
– Raymond Nijland
Nov 14 '18 at 11:12
@MadhurBhaiya PHP
– Gloytos htyqo
Nov 14 '18 at 11:33
add a comment |
i have a mysql table and there i have same text but in different language
ID, Lang, Text, number, chapter
1, English, Hello, 2, 1
4, German, Hallo, 2, 1
5, Spanish, Hola, 2, 1
so how can see those like this
Select * from table where chapter=1 (and language: english,german,spanish)
ID, lang1, lang2, lang3, number, chapter
5, Hello, Hallo, Hola, 2, 1
i want to join columns in 1 row
mysql sql
i have a mysql table and there i have same text but in different language
ID, Lang, Text, number, chapter
1, English, Hello, 2, 1
4, German, Hallo, 2, 1
5, Spanish, Hola, 2, 1
so how can see those like this
Select * from table where chapter=1 (and language: english,german,spanish)
ID, lang1, lang2, lang3, number, chapter
5, Hello, Hallo, Hola, 2, 1
i want to join columns in 1 row
mysql sql
mysql sql
edited Nov 14 '18 at 12:28
D-Shih
25.8k61531
25.8k61531
asked Nov 14 '18 at 11:04
Gloytos htyqoGloytos htyqo
708
708
1
Possible duplicate of MySQL pivot table
– Madhur Bhaiya
Nov 14 '18 at 11:05
Consider handling data display related requirements in your application code (eg: PHP, C++. Java etc)
– Madhur Bhaiya
Nov 14 '18 at 11:05
2
If you don't mind to get onelang
column with a comma separted string you can also consider GROUP_CONCAT (dev.mysql.com/doc/refman/8.0/en/…) instead.. Besides it's more eazy because the pivot query needs to be created dynamic to support more columns.
– Raymond Nijland
Nov 14 '18 at 11:12
@MadhurBhaiya PHP
– Gloytos htyqo
Nov 14 '18 at 11:33
add a comment |
1
Possible duplicate of MySQL pivot table
– Madhur Bhaiya
Nov 14 '18 at 11:05
Consider handling data display related requirements in your application code (eg: PHP, C++. Java etc)
– Madhur Bhaiya
Nov 14 '18 at 11:05
2
If you don't mind to get onelang
column with a comma separted string you can also consider GROUP_CONCAT (dev.mysql.com/doc/refman/8.0/en/…) instead.. Besides it's more eazy because the pivot query needs to be created dynamic to support more columns.
– Raymond Nijland
Nov 14 '18 at 11:12
@MadhurBhaiya PHP
– Gloytos htyqo
Nov 14 '18 at 11:33
1
1
Possible duplicate of MySQL pivot table
– Madhur Bhaiya
Nov 14 '18 at 11:05
Possible duplicate of MySQL pivot table
– Madhur Bhaiya
Nov 14 '18 at 11:05
Consider handling data display related requirements in your application code (eg: PHP, C++. Java etc)
– Madhur Bhaiya
Nov 14 '18 at 11:05
Consider handling data display related requirements in your application code (eg: PHP, C++. Java etc)
– Madhur Bhaiya
Nov 14 '18 at 11:05
2
2
If you don't mind to get one
lang
column with a comma separted string you can also consider GROUP_CONCAT (dev.mysql.com/doc/refman/8.0/en/…) instead.. Besides it's more eazy because the pivot query needs to be created dynamic to support more columns.– Raymond Nijland
Nov 14 '18 at 11:12
If you don't mind to get one
lang
column with a comma separted string you can also consider GROUP_CONCAT (dev.mysql.com/doc/refman/8.0/en/…) instead.. Besides it's more eazy because the pivot query needs to be created dynamic to support more columns.– Raymond Nijland
Nov 14 '18 at 11:12
@MadhurBhaiya PHP
– Gloytos htyqo
Nov 14 '18 at 11:33
@MadhurBhaiya PHP
– Gloytos htyqo
Nov 14 '18 at 11:33
add a comment |
3 Answers
3
active
oldest
votes
You can try to use condition aggregate function to make it.
Schema (MySQL v5.7)
CREATE TABLE T(
ID int,
Lang varchar(50),
Text varchar(50),
number int,
chapter int
);
INSERT INTO T VALUES (1, 'English', 'Hello', 2, 1);
INSERT INTO T VALUES (4, 'German', 'Hallo', 2, 1);
INSERT INTO T VALUES (5, 'Spanish', 'Hola', 2, 1);
Query #1
SELECT MAX(ID) id,
MAX(CASE WHEN Lang ='English' THEN Text END) lang1,
MAX(CASE WHEN Lang ='German' THEN Text END) lang2,
MAX(CASE WHEN Lang ='Spanish' THEN Text END) lang3,
number,
chapter
FROM T
GROUP BY number, chapter;
| id | lang1 | lang2 | lang3 | number | chapter |
| --- | ----- | ----- | ----- | ------ | ------- |
| 5 | Hello | Hallo | Hola | 2 | 1 |
View on DB Fiddle
add a comment |
Create table test :
CREATE TABLE test (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
Lang VARCHAR(30) NOT NULL,
Text VARCHAR(30) NOT NULL,
number VARCHAR(50),
chapter INT(10))
Insert records:
insert into test values(1,'English','Hello','2',1);
insert into test values(4,'German','Hallo','2',1);
insert into test values(5,'Spanish','Hola','2',1);
+----+---------+-------+--------+---------+
| id | Lang | Text | number | chapter |
+----+---------+-------+--------+---------+
| 1 | English | Hello | 2 | 1 |
| 4 | German | Hallo | 2 | 1 |
| 5 | Spanish | Hola | 2 | 1 |
+----+---------+-------+--------+---------+
SELECT MAX(id) id, MAX(CASE WHEN Lang ='English' THEN Text END) lang1,
MAX(CASE WHEN Lang ='German' THEN Text END) lang2,
MAX(CASE WHEN Lang ='Spanish' THEN Text END) lang3, number, chapter
FROM test GROUP BY number, chapter;
+------+-------+-------+-------+--------+---------+
| id | lang1 | lang2 | lang3 | number | chapter |
+------+-------+-------+-------+--------+---------+
| 5 | Hello | Hallo | Hola | 2 | 1 |
+------+-------+-------+-------+--------+---------+
add a comment |
SELECT * FROM
(SELECT LangAS t1, vargu FROM table LANG=114 AND chapter=1) as A,
(SELECT LangAS t2 FROM table WHERE LANG=115 AND chapter=1) AS B,
(SELECT LangAS t3 FROM table WHERE LANG=116 AND chapter=1) AS C;
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%2f53298710%2fmysqli-merge-rows-in-1-based-on-some-parameters%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can try to use condition aggregate function to make it.
Schema (MySQL v5.7)
CREATE TABLE T(
ID int,
Lang varchar(50),
Text varchar(50),
number int,
chapter int
);
INSERT INTO T VALUES (1, 'English', 'Hello', 2, 1);
INSERT INTO T VALUES (4, 'German', 'Hallo', 2, 1);
INSERT INTO T VALUES (5, 'Spanish', 'Hola', 2, 1);
Query #1
SELECT MAX(ID) id,
MAX(CASE WHEN Lang ='English' THEN Text END) lang1,
MAX(CASE WHEN Lang ='German' THEN Text END) lang2,
MAX(CASE WHEN Lang ='Spanish' THEN Text END) lang3,
number,
chapter
FROM T
GROUP BY number, chapter;
| id | lang1 | lang2 | lang3 | number | chapter |
| --- | ----- | ----- | ----- | ------ | ------- |
| 5 | Hello | Hallo | Hola | 2 | 1 |
View on DB Fiddle
add a comment |
You can try to use condition aggregate function to make it.
Schema (MySQL v5.7)
CREATE TABLE T(
ID int,
Lang varchar(50),
Text varchar(50),
number int,
chapter int
);
INSERT INTO T VALUES (1, 'English', 'Hello', 2, 1);
INSERT INTO T VALUES (4, 'German', 'Hallo', 2, 1);
INSERT INTO T VALUES (5, 'Spanish', 'Hola', 2, 1);
Query #1
SELECT MAX(ID) id,
MAX(CASE WHEN Lang ='English' THEN Text END) lang1,
MAX(CASE WHEN Lang ='German' THEN Text END) lang2,
MAX(CASE WHEN Lang ='Spanish' THEN Text END) lang3,
number,
chapter
FROM T
GROUP BY number, chapter;
| id | lang1 | lang2 | lang3 | number | chapter |
| --- | ----- | ----- | ----- | ------ | ------- |
| 5 | Hello | Hallo | Hola | 2 | 1 |
View on DB Fiddle
add a comment |
You can try to use condition aggregate function to make it.
Schema (MySQL v5.7)
CREATE TABLE T(
ID int,
Lang varchar(50),
Text varchar(50),
number int,
chapter int
);
INSERT INTO T VALUES (1, 'English', 'Hello', 2, 1);
INSERT INTO T VALUES (4, 'German', 'Hallo', 2, 1);
INSERT INTO T VALUES (5, 'Spanish', 'Hola', 2, 1);
Query #1
SELECT MAX(ID) id,
MAX(CASE WHEN Lang ='English' THEN Text END) lang1,
MAX(CASE WHEN Lang ='German' THEN Text END) lang2,
MAX(CASE WHEN Lang ='Spanish' THEN Text END) lang3,
number,
chapter
FROM T
GROUP BY number, chapter;
| id | lang1 | lang2 | lang3 | number | chapter |
| --- | ----- | ----- | ----- | ------ | ------- |
| 5 | Hello | Hallo | Hola | 2 | 1 |
View on DB Fiddle
You can try to use condition aggregate function to make it.
Schema (MySQL v5.7)
CREATE TABLE T(
ID int,
Lang varchar(50),
Text varchar(50),
number int,
chapter int
);
INSERT INTO T VALUES (1, 'English', 'Hello', 2, 1);
INSERT INTO T VALUES (4, 'German', 'Hallo', 2, 1);
INSERT INTO T VALUES (5, 'Spanish', 'Hola', 2, 1);
Query #1
SELECT MAX(ID) id,
MAX(CASE WHEN Lang ='English' THEN Text END) lang1,
MAX(CASE WHEN Lang ='German' THEN Text END) lang2,
MAX(CASE WHEN Lang ='Spanish' THEN Text END) lang3,
number,
chapter
FROM T
GROUP BY number, chapter;
| id | lang1 | lang2 | lang3 | number | chapter |
| --- | ----- | ----- | ----- | ------ | ------- |
| 5 | Hello | Hallo | Hola | 2 | 1 |
View on DB Fiddle
answered Nov 14 '18 at 11:54
D-ShihD-Shih
25.8k61531
25.8k61531
add a comment |
add a comment |
Create table test :
CREATE TABLE test (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
Lang VARCHAR(30) NOT NULL,
Text VARCHAR(30) NOT NULL,
number VARCHAR(50),
chapter INT(10))
Insert records:
insert into test values(1,'English','Hello','2',1);
insert into test values(4,'German','Hallo','2',1);
insert into test values(5,'Spanish','Hola','2',1);
+----+---------+-------+--------+---------+
| id | Lang | Text | number | chapter |
+----+---------+-------+--------+---------+
| 1 | English | Hello | 2 | 1 |
| 4 | German | Hallo | 2 | 1 |
| 5 | Spanish | Hola | 2 | 1 |
+----+---------+-------+--------+---------+
SELECT MAX(id) id, MAX(CASE WHEN Lang ='English' THEN Text END) lang1,
MAX(CASE WHEN Lang ='German' THEN Text END) lang2,
MAX(CASE WHEN Lang ='Spanish' THEN Text END) lang3, number, chapter
FROM test GROUP BY number, chapter;
+------+-------+-------+-------+--------+---------+
| id | lang1 | lang2 | lang3 | number | chapter |
+------+-------+-------+-------+--------+---------+
| 5 | Hello | Hallo | Hola | 2 | 1 |
+------+-------+-------+-------+--------+---------+
add a comment |
Create table test :
CREATE TABLE test (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
Lang VARCHAR(30) NOT NULL,
Text VARCHAR(30) NOT NULL,
number VARCHAR(50),
chapter INT(10))
Insert records:
insert into test values(1,'English','Hello','2',1);
insert into test values(4,'German','Hallo','2',1);
insert into test values(5,'Spanish','Hola','2',1);
+----+---------+-------+--------+---------+
| id | Lang | Text | number | chapter |
+----+---------+-------+--------+---------+
| 1 | English | Hello | 2 | 1 |
| 4 | German | Hallo | 2 | 1 |
| 5 | Spanish | Hola | 2 | 1 |
+----+---------+-------+--------+---------+
SELECT MAX(id) id, MAX(CASE WHEN Lang ='English' THEN Text END) lang1,
MAX(CASE WHEN Lang ='German' THEN Text END) lang2,
MAX(CASE WHEN Lang ='Spanish' THEN Text END) lang3, number, chapter
FROM test GROUP BY number, chapter;
+------+-------+-------+-------+--------+---------+
| id | lang1 | lang2 | lang3 | number | chapter |
+------+-------+-------+-------+--------+---------+
| 5 | Hello | Hallo | Hola | 2 | 1 |
+------+-------+-------+-------+--------+---------+
add a comment |
Create table test :
CREATE TABLE test (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
Lang VARCHAR(30) NOT NULL,
Text VARCHAR(30) NOT NULL,
number VARCHAR(50),
chapter INT(10))
Insert records:
insert into test values(1,'English','Hello','2',1);
insert into test values(4,'German','Hallo','2',1);
insert into test values(5,'Spanish','Hola','2',1);
+----+---------+-------+--------+---------+
| id | Lang | Text | number | chapter |
+----+---------+-------+--------+---------+
| 1 | English | Hello | 2 | 1 |
| 4 | German | Hallo | 2 | 1 |
| 5 | Spanish | Hola | 2 | 1 |
+----+---------+-------+--------+---------+
SELECT MAX(id) id, MAX(CASE WHEN Lang ='English' THEN Text END) lang1,
MAX(CASE WHEN Lang ='German' THEN Text END) lang2,
MAX(CASE WHEN Lang ='Spanish' THEN Text END) lang3, number, chapter
FROM test GROUP BY number, chapter;
+------+-------+-------+-------+--------+---------+
| id | lang1 | lang2 | lang3 | number | chapter |
+------+-------+-------+-------+--------+---------+
| 5 | Hello | Hallo | Hola | 2 | 1 |
+------+-------+-------+-------+--------+---------+
Create table test :
CREATE TABLE test (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
Lang VARCHAR(30) NOT NULL,
Text VARCHAR(30) NOT NULL,
number VARCHAR(50),
chapter INT(10))
Insert records:
insert into test values(1,'English','Hello','2',1);
insert into test values(4,'German','Hallo','2',1);
insert into test values(5,'Spanish','Hola','2',1);
+----+---------+-------+--------+---------+
| id | Lang | Text | number | chapter |
+----+---------+-------+--------+---------+
| 1 | English | Hello | 2 | 1 |
| 4 | German | Hallo | 2 | 1 |
| 5 | Spanish | Hola | 2 | 1 |
+----+---------+-------+--------+---------+
SELECT MAX(id) id, MAX(CASE WHEN Lang ='English' THEN Text END) lang1,
MAX(CASE WHEN Lang ='German' THEN Text END) lang2,
MAX(CASE WHEN Lang ='Spanish' THEN Text END) lang3, number, chapter
FROM test GROUP BY number, chapter;
+------+-------+-------+-------+--------+---------+
| id | lang1 | lang2 | lang3 | number | chapter |
+------+-------+-------+-------+--------+---------+
| 5 | Hello | Hallo | Hola | 2 | 1 |
+------+-------+-------+-------+--------+---------+
edited Nov 14 '18 at 12:57
Mihai Chelaru
2,210101122
2,210101122
answered Nov 14 '18 at 12:23
Atul AkabariAtul Akabari
954
954
add a comment |
add a comment |
SELECT * FROM
(SELECT LangAS t1, vargu FROM table LANG=114 AND chapter=1) as A,
(SELECT LangAS t2 FROM table WHERE LANG=115 AND chapter=1) AS B,
(SELECT LangAS t3 FROM table WHERE LANG=116 AND chapter=1) AS C;
add a comment |
SELECT * FROM
(SELECT LangAS t1, vargu FROM table LANG=114 AND chapter=1) as A,
(SELECT LangAS t2 FROM table WHERE LANG=115 AND chapter=1) AS B,
(SELECT LangAS t3 FROM table WHERE LANG=116 AND chapter=1) AS C;
add a comment |
SELECT * FROM
(SELECT LangAS t1, vargu FROM table LANG=114 AND chapter=1) as A,
(SELECT LangAS t2 FROM table WHERE LANG=115 AND chapter=1) AS B,
(SELECT LangAS t3 FROM table WHERE LANG=116 AND chapter=1) AS C;
SELECT * FROM
(SELECT LangAS t1, vargu FROM table LANG=114 AND chapter=1) as A,
(SELECT LangAS t2 FROM table WHERE LANG=115 AND chapter=1) AS B,
(SELECT LangAS t3 FROM table WHERE LANG=116 AND chapter=1) AS C;
answered Nov 14 '18 at 12:05
arlindarlind
931212
931212
add a comment |
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.
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%2f53298710%2fmysqli-merge-rows-in-1-based-on-some-parameters%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
Possible duplicate of MySQL pivot table
– Madhur Bhaiya
Nov 14 '18 at 11:05
Consider handling data display related requirements in your application code (eg: PHP, C++. Java etc)
– Madhur Bhaiya
Nov 14 '18 at 11:05
2
If you don't mind to get one
lang
column with a comma separted string you can also consider GROUP_CONCAT (dev.mysql.com/doc/refman/8.0/en/…) instead.. Besides it's more eazy because the pivot query needs to be created dynamic to support more columns.– Raymond Nijland
Nov 14 '18 at 11:12
@MadhurBhaiya PHP
– Gloytos htyqo
Nov 14 '18 at 11:33