Combining Data in Two Tables SQL
up vote
0
down vote
favorite
I'm sure a very basic question, but I'm continue to be stuck:
Table A - image_number, camera_type, total_sales
Table B - image_number, keyword
Table A has one ROW for each image_number - example:
image_number="AXJ789, camera_type="Nikon", total_sales=678
image_number="JIJ123", camera_type="Canon", total_sales=999
image_number="KNI908", camera_type="Sony", total_sales=565
Table B has many ROWs for each image_number - example:
image_number="AXJ789", keyword = "rain"
image_number="AXJ789", keyword = "mountain"
image_number="AXJ789", keyword = "grass"
image_number="AXJ789", keyword = "cloud"
What I'm trying to do is JOIN the two tables so that I can generate the following output:
image_number="AXJ789", camera_type=678, camera_type="Nikon", keyword(1) = "rain", keyword(2) = "mountain", keyword(3) = "grass", keyword(4) = "cloud"
In other words, I want to have all items in each ROW in table A + all the items from table B. For each image_number in Table A, there could be no "keywords" in Table B or 50 keywords - depends on the image.
When I do an INNER JOIN, of course I can get one "keyword" from table B, but I can't figure out how to get all of them.
mysql sql join
add a comment |
up vote
0
down vote
favorite
I'm sure a very basic question, but I'm continue to be stuck:
Table A - image_number, camera_type, total_sales
Table B - image_number, keyword
Table A has one ROW for each image_number - example:
image_number="AXJ789, camera_type="Nikon", total_sales=678
image_number="JIJ123", camera_type="Canon", total_sales=999
image_number="KNI908", camera_type="Sony", total_sales=565
Table B has many ROWs for each image_number - example:
image_number="AXJ789", keyword = "rain"
image_number="AXJ789", keyword = "mountain"
image_number="AXJ789", keyword = "grass"
image_number="AXJ789", keyword = "cloud"
What I'm trying to do is JOIN the two tables so that I can generate the following output:
image_number="AXJ789", camera_type=678, camera_type="Nikon", keyword(1) = "rain", keyword(2) = "mountain", keyword(3) = "grass", keyword(4) = "cloud"
In other words, I want to have all items in each ROW in table A + all the items from table B. For each image_number in Table A, there could be no "keywords" in Table B or 50 keywords - depends on the image.
When I do an INNER JOIN, of course I can get one "keyword" from table B, but I can't figure out how to get all of them.
mysql sql join
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm sure a very basic question, but I'm continue to be stuck:
Table A - image_number, camera_type, total_sales
Table B - image_number, keyword
Table A has one ROW for each image_number - example:
image_number="AXJ789, camera_type="Nikon", total_sales=678
image_number="JIJ123", camera_type="Canon", total_sales=999
image_number="KNI908", camera_type="Sony", total_sales=565
Table B has many ROWs for each image_number - example:
image_number="AXJ789", keyword = "rain"
image_number="AXJ789", keyword = "mountain"
image_number="AXJ789", keyword = "grass"
image_number="AXJ789", keyword = "cloud"
What I'm trying to do is JOIN the two tables so that I can generate the following output:
image_number="AXJ789", camera_type=678, camera_type="Nikon", keyword(1) = "rain", keyword(2) = "mountain", keyword(3) = "grass", keyword(4) = "cloud"
In other words, I want to have all items in each ROW in table A + all the items from table B. For each image_number in Table A, there could be no "keywords" in Table B or 50 keywords - depends on the image.
When I do an INNER JOIN, of course I can get one "keyword" from table B, but I can't figure out how to get all of them.
mysql sql join
I'm sure a very basic question, but I'm continue to be stuck:
Table A - image_number, camera_type, total_sales
Table B - image_number, keyword
Table A has one ROW for each image_number - example:
image_number="AXJ789, camera_type="Nikon", total_sales=678
image_number="JIJ123", camera_type="Canon", total_sales=999
image_number="KNI908", camera_type="Sony", total_sales=565
Table B has many ROWs for each image_number - example:
image_number="AXJ789", keyword = "rain"
image_number="AXJ789", keyword = "mountain"
image_number="AXJ789", keyword = "grass"
image_number="AXJ789", keyword = "cloud"
What I'm trying to do is JOIN the two tables so that I can generate the following output:
image_number="AXJ789", camera_type=678, camera_type="Nikon", keyword(1) = "rain", keyword(2) = "mountain", keyword(3) = "grass", keyword(4) = "cloud"
In other words, I want to have all items in each ROW in table A + all the items from table B. For each image_number in Table A, there could be no "keywords" in Table B or 50 keywords - depends on the image.
When I do an INNER JOIN, of course I can get one "keyword" from table B, but I can't figure out how to get all of them.
mysql sql join
mysql sql join
edited Nov 19 at 2:28
kit
986316
986316
asked Nov 19 at 0:39
Rick Leckrone
54
54
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
You can generate a comma-separated list of keywords for each image using GROUP_CONCAT and JOIN (but use a LEFT JOIN if an image may have no keywords).
SELECT a.*, GROUP_CONCAT(b.keyword) AS keyword_list
FROM a
JOIN b on b.image_number = a.image_number
GROUP BY a.image_number
Output for your sample data:
image_number camera_type total_sales keyword_list
AXJ789 Nikon 678 rain,mountain,grass,cloud
Demo on dbfiddle
You can then parse this into an array in your application, for example in PHP (if you have read the row into $row):
$keywords = explode(',', $row['keyword_list']);
print_r($keywords);
Output:
Array
(
[0] => rain
[1] => mountain
[2] => grass
[3] => cloud
)
Yeah. Concat. Makes sense. I woke up last night realizing that I could dump to excel and then concat and then dump back in to Table A. This is super clear. Thank you so much.
– Rick Leckrone
Nov 20 at 0:34
Worked perfectly!
– Rick Leckrone
Nov 20 at 1:20
That's great. Glad I could help.
– Nick
Nov 20 at 1:53
add a comment |
up vote
1
down vote
You can concatenate the keywords together:
select a.*,
(select group_concat(b.keyword)
from b
where b.image_number = a. image_number
) as keywords
from a;
This creates a comma-delimited list of the keywords. This is much simpler (in MySQL) than trying to put them in separate columns. In fact, if you wanted separate columns, I might suggest parsing this result:
select a.*, -- or whatever columns you want
substring_index(keywords, ',' 1) as keyword1,
substring_index(substring_index(keywords, ',' 2), ',', -1) as keyword2,
substring_index(substring_index(keywords, ',' 3), ',', -1) as keyword3,
substring_index(substring_index(keywords, ',' 4), ',', -1) as keyword4
from a left join
(select b.image_number, group_concat(b.keyword) as keywords
from b
group by b.image_number
) b
on b.image_number = a. image_number;
Thank you so much. Will give it a spin.
– Rick Leckrone
Nov 20 at 0:32
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You can generate a comma-separated list of keywords for each image using GROUP_CONCAT and JOIN (but use a LEFT JOIN if an image may have no keywords).
SELECT a.*, GROUP_CONCAT(b.keyword) AS keyword_list
FROM a
JOIN b on b.image_number = a.image_number
GROUP BY a.image_number
Output for your sample data:
image_number camera_type total_sales keyword_list
AXJ789 Nikon 678 rain,mountain,grass,cloud
Demo on dbfiddle
You can then parse this into an array in your application, for example in PHP (if you have read the row into $row):
$keywords = explode(',', $row['keyword_list']);
print_r($keywords);
Output:
Array
(
[0] => rain
[1] => mountain
[2] => grass
[3] => cloud
)
Yeah. Concat. Makes sense. I woke up last night realizing that I could dump to excel and then concat and then dump back in to Table A. This is super clear. Thank you so much.
– Rick Leckrone
Nov 20 at 0:34
Worked perfectly!
– Rick Leckrone
Nov 20 at 1:20
That's great. Glad I could help.
– Nick
Nov 20 at 1:53
add a comment |
up vote
0
down vote
accepted
You can generate a comma-separated list of keywords for each image using GROUP_CONCAT and JOIN (but use a LEFT JOIN if an image may have no keywords).
SELECT a.*, GROUP_CONCAT(b.keyword) AS keyword_list
FROM a
JOIN b on b.image_number = a.image_number
GROUP BY a.image_number
Output for your sample data:
image_number camera_type total_sales keyword_list
AXJ789 Nikon 678 rain,mountain,grass,cloud
Demo on dbfiddle
You can then parse this into an array in your application, for example in PHP (if you have read the row into $row):
$keywords = explode(',', $row['keyword_list']);
print_r($keywords);
Output:
Array
(
[0] => rain
[1] => mountain
[2] => grass
[3] => cloud
)
Yeah. Concat. Makes sense. I woke up last night realizing that I could dump to excel and then concat and then dump back in to Table A. This is super clear. Thank you so much.
– Rick Leckrone
Nov 20 at 0:34
Worked perfectly!
– Rick Leckrone
Nov 20 at 1:20
That's great. Glad I could help.
– Nick
Nov 20 at 1:53
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You can generate a comma-separated list of keywords for each image using GROUP_CONCAT and JOIN (but use a LEFT JOIN if an image may have no keywords).
SELECT a.*, GROUP_CONCAT(b.keyword) AS keyword_list
FROM a
JOIN b on b.image_number = a.image_number
GROUP BY a.image_number
Output for your sample data:
image_number camera_type total_sales keyword_list
AXJ789 Nikon 678 rain,mountain,grass,cloud
Demo on dbfiddle
You can then parse this into an array in your application, for example in PHP (if you have read the row into $row):
$keywords = explode(',', $row['keyword_list']);
print_r($keywords);
Output:
Array
(
[0] => rain
[1] => mountain
[2] => grass
[3] => cloud
)
You can generate a comma-separated list of keywords for each image using GROUP_CONCAT and JOIN (but use a LEFT JOIN if an image may have no keywords).
SELECT a.*, GROUP_CONCAT(b.keyword) AS keyword_list
FROM a
JOIN b on b.image_number = a.image_number
GROUP BY a.image_number
Output for your sample data:
image_number camera_type total_sales keyword_list
AXJ789 Nikon 678 rain,mountain,grass,cloud
Demo on dbfiddle
You can then parse this into an array in your application, for example in PHP (if you have read the row into $row):
$keywords = explode(',', $row['keyword_list']);
print_r($keywords);
Output:
Array
(
[0] => rain
[1] => mountain
[2] => grass
[3] => cloud
)
edited Nov 19 at 1:17
answered Nov 19 at 1:00
Nick
20.1k51434
20.1k51434
Yeah. Concat. Makes sense. I woke up last night realizing that I could dump to excel and then concat and then dump back in to Table A. This is super clear. Thank you so much.
– Rick Leckrone
Nov 20 at 0:34
Worked perfectly!
– Rick Leckrone
Nov 20 at 1:20
That's great. Glad I could help.
– Nick
Nov 20 at 1:53
add a comment |
Yeah. Concat. Makes sense. I woke up last night realizing that I could dump to excel and then concat and then dump back in to Table A. This is super clear. Thank you so much.
– Rick Leckrone
Nov 20 at 0:34
Worked perfectly!
– Rick Leckrone
Nov 20 at 1:20
That's great. Glad I could help.
– Nick
Nov 20 at 1:53
Yeah. Concat. Makes sense. I woke up last night realizing that I could dump to excel and then concat and then dump back in to Table A. This is super clear. Thank you so much.
– Rick Leckrone
Nov 20 at 0:34
Yeah. Concat. Makes sense. I woke up last night realizing that I could dump to excel and then concat and then dump back in to Table A. This is super clear. Thank you so much.
– Rick Leckrone
Nov 20 at 0:34
Worked perfectly!
– Rick Leckrone
Nov 20 at 1:20
Worked perfectly!
– Rick Leckrone
Nov 20 at 1:20
That's great. Glad I could help.
– Nick
Nov 20 at 1:53
That's great. Glad I could help.
– Nick
Nov 20 at 1:53
add a comment |
up vote
1
down vote
You can concatenate the keywords together:
select a.*,
(select group_concat(b.keyword)
from b
where b.image_number = a. image_number
) as keywords
from a;
This creates a comma-delimited list of the keywords. This is much simpler (in MySQL) than trying to put them in separate columns. In fact, if you wanted separate columns, I might suggest parsing this result:
select a.*, -- or whatever columns you want
substring_index(keywords, ',' 1) as keyword1,
substring_index(substring_index(keywords, ',' 2), ',', -1) as keyword2,
substring_index(substring_index(keywords, ',' 3), ',', -1) as keyword3,
substring_index(substring_index(keywords, ',' 4), ',', -1) as keyword4
from a left join
(select b.image_number, group_concat(b.keyword) as keywords
from b
group by b.image_number
) b
on b.image_number = a. image_number;
Thank you so much. Will give it a spin.
– Rick Leckrone
Nov 20 at 0:32
add a comment |
up vote
1
down vote
You can concatenate the keywords together:
select a.*,
(select group_concat(b.keyword)
from b
where b.image_number = a. image_number
) as keywords
from a;
This creates a comma-delimited list of the keywords. This is much simpler (in MySQL) than trying to put them in separate columns. In fact, if you wanted separate columns, I might suggest parsing this result:
select a.*, -- or whatever columns you want
substring_index(keywords, ',' 1) as keyword1,
substring_index(substring_index(keywords, ',' 2), ',', -1) as keyword2,
substring_index(substring_index(keywords, ',' 3), ',', -1) as keyword3,
substring_index(substring_index(keywords, ',' 4), ',', -1) as keyword4
from a left join
(select b.image_number, group_concat(b.keyword) as keywords
from b
group by b.image_number
) b
on b.image_number = a. image_number;
Thank you so much. Will give it a spin.
– Rick Leckrone
Nov 20 at 0:32
add a comment |
up vote
1
down vote
up vote
1
down vote
You can concatenate the keywords together:
select a.*,
(select group_concat(b.keyword)
from b
where b.image_number = a. image_number
) as keywords
from a;
This creates a comma-delimited list of the keywords. This is much simpler (in MySQL) than trying to put them in separate columns. In fact, if you wanted separate columns, I might suggest parsing this result:
select a.*, -- or whatever columns you want
substring_index(keywords, ',' 1) as keyword1,
substring_index(substring_index(keywords, ',' 2), ',', -1) as keyword2,
substring_index(substring_index(keywords, ',' 3), ',', -1) as keyword3,
substring_index(substring_index(keywords, ',' 4), ',', -1) as keyword4
from a left join
(select b.image_number, group_concat(b.keyword) as keywords
from b
group by b.image_number
) b
on b.image_number = a. image_number;
You can concatenate the keywords together:
select a.*,
(select group_concat(b.keyword)
from b
where b.image_number = a. image_number
) as keywords
from a;
This creates a comma-delimited list of the keywords. This is much simpler (in MySQL) than trying to put them in separate columns. In fact, if you wanted separate columns, I might suggest parsing this result:
select a.*, -- or whatever columns you want
substring_index(keywords, ',' 1) as keyword1,
substring_index(substring_index(keywords, ',' 2), ',', -1) as keyword2,
substring_index(substring_index(keywords, ',' 3), ',', -1) as keyword3,
substring_index(substring_index(keywords, ',' 4), ',', -1) as keyword4
from a left join
(select b.image_number, group_concat(b.keyword) as keywords
from b
group by b.image_number
) b
on b.image_number = a. image_number;
answered Nov 19 at 0:43
Gordon Linoff
746k33285390
746k33285390
Thank you so much. Will give it a spin.
– Rick Leckrone
Nov 20 at 0:32
add a comment |
Thank you so much. Will give it a spin.
– Rick Leckrone
Nov 20 at 0:32
Thank you so much. Will give it a spin.
– Rick Leckrone
Nov 20 at 0:32
Thank you so much. Will give it a spin.
– Rick Leckrone
Nov 20 at 0:32
add a comment |
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%2f53366927%2fcombining-data-in-two-tables-sql%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