MySQL Stored Procedure to loop through 2 tables, select values, and store in 3rd
I have 3 tables users
, memos
, and memos_read
.
users
:
+---+----------+
|ID | NAME |
+---+----------+
| 1 | Mike |
| 4 | Susan |
| 5 | Peter |
| 6 | Mary |
+---+----------+
memos
:
+---+----------+
|ID | CONTENT |
+---+----------+
| 15| Hello |
| 16| World |
| 17| Foo |
| 23| Bar |
+---+----------+
I want to create a stored procedure with a nested loop to create a "read map" for memos that have been read, creating something like:
memos_read
:
+---------+----------+
| MEMO_ID | USER_ID |
+---------+----------+
| 15 | 1 |
| 15 | 4 |
| 15 | 5 |
| 15 | 6 |
| 16 | 1 |
| 16 | 4 |
| 16 | 5 |
| 16 | 6 |
| 17 | 1 |
| 17 | 4 |
| 17 | 5 |
| 17 | 6 |
| 23 | 1 |
| 23 | 4 |
| 23 | 5 |
| 23 | 6 |
+---------+----------+
Is this possible? Thank you in advanced!
mysql stored-procedures
add a comment |
I have 3 tables users
, memos
, and memos_read
.
users
:
+---+----------+
|ID | NAME |
+---+----------+
| 1 | Mike |
| 4 | Susan |
| 5 | Peter |
| 6 | Mary |
+---+----------+
memos
:
+---+----------+
|ID | CONTENT |
+---+----------+
| 15| Hello |
| 16| World |
| 17| Foo |
| 23| Bar |
+---+----------+
I want to create a stored procedure with a nested loop to create a "read map" for memos that have been read, creating something like:
memos_read
:
+---------+----------+
| MEMO_ID | USER_ID |
+---------+----------+
| 15 | 1 |
| 15 | 4 |
| 15 | 5 |
| 15 | 6 |
| 16 | 1 |
| 16 | 4 |
| 16 | 5 |
| 16 | 6 |
| 17 | 1 |
| 17 | 4 |
| 17 | 5 |
| 17 | 6 |
| 23 | 1 |
| 23 | 4 |
| 23 | 5 |
| 23 | 6 |
+---------+----------+
Is this possible? Thank you in advanced!
mysql stored-procedures
1
That's just a cross join no sp required.
– P.Salmon
Nov 23 '18 at 6:36
add a comment |
I have 3 tables users
, memos
, and memos_read
.
users
:
+---+----------+
|ID | NAME |
+---+----------+
| 1 | Mike |
| 4 | Susan |
| 5 | Peter |
| 6 | Mary |
+---+----------+
memos
:
+---+----------+
|ID | CONTENT |
+---+----------+
| 15| Hello |
| 16| World |
| 17| Foo |
| 23| Bar |
+---+----------+
I want to create a stored procedure with a nested loop to create a "read map" for memos that have been read, creating something like:
memos_read
:
+---------+----------+
| MEMO_ID | USER_ID |
+---------+----------+
| 15 | 1 |
| 15 | 4 |
| 15 | 5 |
| 15 | 6 |
| 16 | 1 |
| 16 | 4 |
| 16 | 5 |
| 16 | 6 |
| 17 | 1 |
| 17 | 4 |
| 17 | 5 |
| 17 | 6 |
| 23 | 1 |
| 23 | 4 |
| 23 | 5 |
| 23 | 6 |
+---------+----------+
Is this possible? Thank you in advanced!
mysql stored-procedures
I have 3 tables users
, memos
, and memos_read
.
users
:
+---+----------+
|ID | NAME |
+---+----------+
| 1 | Mike |
| 4 | Susan |
| 5 | Peter |
| 6 | Mary |
+---+----------+
memos
:
+---+----------+
|ID | CONTENT |
+---+----------+
| 15| Hello |
| 16| World |
| 17| Foo |
| 23| Bar |
+---+----------+
I want to create a stored procedure with a nested loop to create a "read map" for memos that have been read, creating something like:
memos_read
:
+---------+----------+
| MEMO_ID | USER_ID |
+---------+----------+
| 15 | 1 |
| 15 | 4 |
| 15 | 5 |
| 15 | 6 |
| 16 | 1 |
| 16 | 4 |
| 16 | 5 |
| 16 | 6 |
| 17 | 1 |
| 17 | 4 |
| 17 | 5 |
| 17 | 6 |
| 23 | 1 |
| 23 | 4 |
| 23 | 5 |
| 23 | 6 |
+---------+----------+
Is this possible? Thank you in advanced!
mysql stored-procedures
mysql stored-procedures
asked Nov 23 '18 at 6:21
Mike FengMike Feng
3791313
3791313
1
That's just a cross join no sp required.
– P.Salmon
Nov 23 '18 at 6:36
add a comment |
1
That's just a cross join no sp required.
– P.Salmon
Nov 23 '18 at 6:36
1
1
That's just a cross join no sp required.
– P.Salmon
Nov 23 '18 at 6:36
That's just a cross join no sp required.
– P.Salmon
Nov 23 '18 at 6:36
add a comment |
2 Answers
2
active
oldest
votes
@Nick's answer should work for you; however if you are not interested in creating a new table, and rather use the "read map" dynamically inside a query; you can utilize a Derived Table.
General query structure would be something as follows:
SELECT
read_map.MEMO_ID,
read_map.USER_ID,
...
FROM
(
SELECT
m.ID AS MEMO_ID,
u.ID AS USER_ID
FROM users AS u
CROSS JOIN memos AS m
) AS read_map
... -- here you can join with other tables as required
WHERE ...
add a comment |
You can use CREATE TABLE ... SELECT
syntax to create the memos_read
table:
CREATE TABLE memos_read AS
SELECT m.ID AS MEMO_ID, u.ID AS USER_ID
FROM memos m
CROSS JOIN users u
Output is as you requested. Demo on dbfiddle
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%2f53441521%2fmysql-stored-procedure-to-loop-through-2-tables-select-values-and-store-in-3rd%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
@Nick's answer should work for you; however if you are not interested in creating a new table, and rather use the "read map" dynamically inside a query; you can utilize a Derived Table.
General query structure would be something as follows:
SELECT
read_map.MEMO_ID,
read_map.USER_ID,
...
FROM
(
SELECT
m.ID AS MEMO_ID,
u.ID AS USER_ID
FROM users AS u
CROSS JOIN memos AS m
) AS read_map
... -- here you can join with other tables as required
WHERE ...
add a comment |
@Nick's answer should work for you; however if you are not interested in creating a new table, and rather use the "read map" dynamically inside a query; you can utilize a Derived Table.
General query structure would be something as follows:
SELECT
read_map.MEMO_ID,
read_map.USER_ID,
...
FROM
(
SELECT
m.ID AS MEMO_ID,
u.ID AS USER_ID
FROM users AS u
CROSS JOIN memos AS m
) AS read_map
... -- here you can join with other tables as required
WHERE ...
add a comment |
@Nick's answer should work for you; however if you are not interested in creating a new table, and rather use the "read map" dynamically inside a query; you can utilize a Derived Table.
General query structure would be something as follows:
SELECT
read_map.MEMO_ID,
read_map.USER_ID,
...
FROM
(
SELECT
m.ID AS MEMO_ID,
u.ID AS USER_ID
FROM users AS u
CROSS JOIN memos AS m
) AS read_map
... -- here you can join with other tables as required
WHERE ...
@Nick's answer should work for you; however if you are not interested in creating a new table, and rather use the "read map" dynamically inside a query; you can utilize a Derived Table.
General query structure would be something as follows:
SELECT
read_map.MEMO_ID,
read_map.USER_ID,
...
FROM
(
SELECT
m.ID AS MEMO_ID,
u.ID AS USER_ID
FROM users AS u
CROSS JOIN memos AS m
) AS read_map
... -- here you can join with other tables as required
WHERE ...
answered Nov 23 '18 at 7:25
Madhur BhaiyaMadhur Bhaiya
19.6k62236
19.6k62236
add a comment |
add a comment |
You can use CREATE TABLE ... SELECT
syntax to create the memos_read
table:
CREATE TABLE memos_read AS
SELECT m.ID AS MEMO_ID, u.ID AS USER_ID
FROM memos m
CROSS JOIN users u
Output is as you requested. Demo on dbfiddle
add a comment |
You can use CREATE TABLE ... SELECT
syntax to create the memos_read
table:
CREATE TABLE memos_read AS
SELECT m.ID AS MEMO_ID, u.ID AS USER_ID
FROM memos m
CROSS JOIN users u
Output is as you requested. Demo on dbfiddle
add a comment |
You can use CREATE TABLE ... SELECT
syntax to create the memos_read
table:
CREATE TABLE memos_read AS
SELECT m.ID AS MEMO_ID, u.ID AS USER_ID
FROM memos m
CROSS JOIN users u
Output is as you requested. Demo on dbfiddle
You can use CREATE TABLE ... SELECT
syntax to create the memos_read
table:
CREATE TABLE memos_read AS
SELECT m.ID AS MEMO_ID, u.ID AS USER_ID
FROM memos m
CROSS JOIN users u
Output is as you requested. Demo on dbfiddle
answered Nov 23 '18 at 6:47
NickNick
27.4k111941
27.4k111941
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%2f53441521%2fmysql-stored-procedure-to-loop-through-2-tables-select-values-and-store-in-3rd%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
That's just a cross join no sp required.
– P.Salmon
Nov 23 '18 at 6:36