mysql understanding group by with a joined tableShould I use the datetime or timestamp data type in MySQL?How to get a list of user accounts using the command line in MySQL?Retrieving the last record in each group - MySQLInsert into a MySQL table or update if existsWhat's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?How to get the sizes of the tables of a MySQL database?Which MySQL Join query is preferable for this situation?How to import an SQL file using the command line in MySQL?Foreach select based in two tables MYSQLMysql Use Column Value In One Table If Join Doesn't Exist To Second Table
Where is the logic in castrating fighters?
What is quasi-aromaticity?
what kind of chord progession is this?
What is the largest (size) solid object ever dropped from an airplane to impact the ground in freefall?
Gladys goes shopping
My players want to grind XP but we're using milestone advancement
Python program to calculate what pawns are safe
Is DateWithin30Days(Date 1, Date 2) an Apex Method?
A steel cutting sword?
I unknowingly submitted plagarised work
C++ forcing function parameter evalution order
What to do when you've set the wrong ISO for your film?
Did the UK Government ask for the Irish backstop?
How did these characters "suit up" so quickly?
What does this symbol on the box of power supply mean?
How strong are Wi-Fi signals?
Using credit/debit card details vs swiping a card in a payment (credit card) terminal
What is this opening/position called, and what is white’s best move?
Pirate democracy at its finest
Any advice on creating fictional locations in real places when writing historical fiction?
Compaq Portable vs IBM 5155 Portable PC
What are the real benefits of using Salesforce DX?
Where have Brexit voters gone?
Should one buy new hardware after a system compromise?
mysql understanding group by with a joined table
Should I use the datetime or timestamp data type in MySQL?How to get a list of user accounts using the command line in MySQL?Retrieving the last record in each group - MySQLInsert into a MySQL table or update if existsWhat's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?How to get the sizes of the tables of a MySQL database?Which MySQL Join query is preferable for this situation?How to import an SQL file using the command line in MySQL?Foreach select based in two tables MYSQLMysql Use Column Value In One Table If Join Doesn't Exist To Second Table
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
So I have the "main" table (A) with fields: id, order_number, order_name
and table (B) with fields: id, fk.order_number, tracking_number
Table (A) is responsible for keeping track of each order, while table (B) stores all associated tracking information per each order.
What I am trying to accomplish is to query each order from table A and join table B to show the first tracking number that has been stored for each order, almost like a limit 1 (return only the first stored tracking number for each order).
How I am doing this currently is a join between table A and table B on the order_number field, but I am using the GROUP BY tableA.order_number
at the end of the statement.
select tablea.order_number, tablea.order_name, tableb.tracking_number
from tablea
join table b
on tablea.order_number = tableb.order_number
group by tablea.order_number
I guess the question revolves around, what is the default group by ordering when you return multiple rows back from the joined table?
For example, in table A, there is only 1 row, while in tale B there are 2 rows (2 tracking numbers for the order). So, when I group by in this case, does that always take the first match from the joined table where the condition matches the group by? If I removed group by, 2 rows would be returned.
I realize what is happening because I have the group by condition on tableA, and so it only shows the first row because both rows returned from the join have the same order number, which appears to be what I want (limit 1 tracking number per order), but I'm not sure if programmatically I actually did this correctly or if it happens this way because that is how the group by clause works and how I used it here. I just want to limit 1 tracking number from the tableB based on the order_number of table A.
Updated (with example query)
SELECT m.message_id, m.message_date, m.message_order_number, m.message_purchase_order, m.message_vendor_invoice, ve.vendor_email_display, concat(c.customer_first_name, ' ', c.customer_last_name) as customer_name,
min(ti.tracking_information_id) as tracking_information_id, ti.tracking_information_tracking_number, ti.tracking_information_tracking_number_status
FROM email.message m
JOIN email.customer c
ON m.message_tagged_customer_first = c.customer_id and m.message_tagged_customer_last = c.customer_id
JOIN vendor_email ve
ON m.message_sender = ve.vendor_email_id
LEFT JOIN tracking_information ti
ON m.message_order_number = ti.tracking_information_order_number
group by m.message_order_number
In this case, I want to return all information in message table, and the first matching row in table tracking
mysql
add a comment |
So I have the "main" table (A) with fields: id, order_number, order_name
and table (B) with fields: id, fk.order_number, tracking_number
Table (A) is responsible for keeping track of each order, while table (B) stores all associated tracking information per each order.
What I am trying to accomplish is to query each order from table A and join table B to show the first tracking number that has been stored for each order, almost like a limit 1 (return only the first stored tracking number for each order).
How I am doing this currently is a join between table A and table B on the order_number field, but I am using the GROUP BY tableA.order_number
at the end of the statement.
select tablea.order_number, tablea.order_name, tableb.tracking_number
from tablea
join table b
on tablea.order_number = tableb.order_number
group by tablea.order_number
I guess the question revolves around, what is the default group by ordering when you return multiple rows back from the joined table?
For example, in table A, there is only 1 row, while in tale B there are 2 rows (2 tracking numbers for the order). So, when I group by in this case, does that always take the first match from the joined table where the condition matches the group by? If I removed group by, 2 rows would be returned.
I realize what is happening because I have the group by condition on tableA, and so it only shows the first row because both rows returned from the join have the same order number, which appears to be what I want (limit 1 tracking number per order), but I'm not sure if programmatically I actually did this correctly or if it happens this way because that is how the group by clause works and how I used it here. I just want to limit 1 tracking number from the tableB based on the order_number of table A.
Updated (with example query)
SELECT m.message_id, m.message_date, m.message_order_number, m.message_purchase_order, m.message_vendor_invoice, ve.vendor_email_display, concat(c.customer_first_name, ' ', c.customer_last_name) as customer_name,
min(ti.tracking_information_id) as tracking_information_id, ti.tracking_information_tracking_number, ti.tracking_information_tracking_number_status
FROM email.message m
JOIN email.customer c
ON m.message_tagged_customer_first = c.customer_id and m.message_tagged_customer_last = c.customer_id
JOIN vendor_email ve
ON m.message_sender = ve.vendor_email_id
LEFT JOIN tracking_information ti
ON m.message_order_number = ti.tracking_information_order_number
group by m.message_order_number
In this case, I want to return all information in message table, and the first matching row in table tracking
mysql
Do tracking numbers always increase?
– Nick
Mar 24 at 4:32
I'm not sure. There are 3 different formats being stored, but I think they are all uniquely created, and not incremental.
– dataviews
Mar 24 at 5:51
3
I guess the question is, in the absence of any aggregating functions, when should you use a group by clause. The answer is: never.
– Strawberry
Mar 24 at 6:42
There is no default behaviour here. It's completely arbitrary. You could get the first row every time, then get the second in testing. Don't use this.
– MatBailie
Mar 24 at 8:23
all, please check my updated question. I put my new query inside.
– dataviews
Mar 24 at 15:15
add a comment |
So I have the "main" table (A) with fields: id, order_number, order_name
and table (B) with fields: id, fk.order_number, tracking_number
Table (A) is responsible for keeping track of each order, while table (B) stores all associated tracking information per each order.
What I am trying to accomplish is to query each order from table A and join table B to show the first tracking number that has been stored for each order, almost like a limit 1 (return only the first stored tracking number for each order).
How I am doing this currently is a join between table A and table B on the order_number field, but I am using the GROUP BY tableA.order_number
at the end of the statement.
select tablea.order_number, tablea.order_name, tableb.tracking_number
from tablea
join table b
on tablea.order_number = tableb.order_number
group by tablea.order_number
I guess the question revolves around, what is the default group by ordering when you return multiple rows back from the joined table?
For example, in table A, there is only 1 row, while in tale B there are 2 rows (2 tracking numbers for the order). So, when I group by in this case, does that always take the first match from the joined table where the condition matches the group by? If I removed group by, 2 rows would be returned.
I realize what is happening because I have the group by condition on tableA, and so it only shows the first row because both rows returned from the join have the same order number, which appears to be what I want (limit 1 tracking number per order), but I'm not sure if programmatically I actually did this correctly or if it happens this way because that is how the group by clause works and how I used it here. I just want to limit 1 tracking number from the tableB based on the order_number of table A.
Updated (with example query)
SELECT m.message_id, m.message_date, m.message_order_number, m.message_purchase_order, m.message_vendor_invoice, ve.vendor_email_display, concat(c.customer_first_name, ' ', c.customer_last_name) as customer_name,
min(ti.tracking_information_id) as tracking_information_id, ti.tracking_information_tracking_number, ti.tracking_information_tracking_number_status
FROM email.message m
JOIN email.customer c
ON m.message_tagged_customer_first = c.customer_id and m.message_tagged_customer_last = c.customer_id
JOIN vendor_email ve
ON m.message_sender = ve.vendor_email_id
LEFT JOIN tracking_information ti
ON m.message_order_number = ti.tracking_information_order_number
group by m.message_order_number
In this case, I want to return all information in message table, and the first matching row in table tracking
mysql
So I have the "main" table (A) with fields: id, order_number, order_name
and table (B) with fields: id, fk.order_number, tracking_number
Table (A) is responsible for keeping track of each order, while table (B) stores all associated tracking information per each order.
What I am trying to accomplish is to query each order from table A and join table B to show the first tracking number that has been stored for each order, almost like a limit 1 (return only the first stored tracking number for each order).
How I am doing this currently is a join between table A and table B on the order_number field, but I am using the GROUP BY tableA.order_number
at the end of the statement.
select tablea.order_number, tablea.order_name, tableb.tracking_number
from tablea
join table b
on tablea.order_number = tableb.order_number
group by tablea.order_number
I guess the question revolves around, what is the default group by ordering when you return multiple rows back from the joined table?
For example, in table A, there is only 1 row, while in tale B there are 2 rows (2 tracking numbers for the order). So, when I group by in this case, does that always take the first match from the joined table where the condition matches the group by? If I removed group by, 2 rows would be returned.
I realize what is happening because I have the group by condition on tableA, and so it only shows the first row because both rows returned from the join have the same order number, which appears to be what I want (limit 1 tracking number per order), but I'm not sure if programmatically I actually did this correctly or if it happens this way because that is how the group by clause works and how I used it here. I just want to limit 1 tracking number from the tableB based on the order_number of table A.
Updated (with example query)
SELECT m.message_id, m.message_date, m.message_order_number, m.message_purchase_order, m.message_vendor_invoice, ve.vendor_email_display, concat(c.customer_first_name, ' ', c.customer_last_name) as customer_name,
min(ti.tracking_information_id) as tracking_information_id, ti.tracking_information_tracking_number, ti.tracking_information_tracking_number_status
FROM email.message m
JOIN email.customer c
ON m.message_tagged_customer_first = c.customer_id and m.message_tagged_customer_last = c.customer_id
JOIN vendor_email ve
ON m.message_sender = ve.vendor_email_id
LEFT JOIN tracking_information ti
ON m.message_order_number = ti.tracking_information_order_number
group by m.message_order_number
In this case, I want to return all information in message table, and the first matching row in table tracking
mysql
mysql
edited Mar 24 at 15:16
dataviews
asked Mar 24 at 4:23
dataviewsdataviews
19814
19814
Do tracking numbers always increase?
– Nick
Mar 24 at 4:32
I'm not sure. There are 3 different formats being stored, but I think they are all uniquely created, and not incremental.
– dataviews
Mar 24 at 5:51
3
I guess the question is, in the absence of any aggregating functions, when should you use a group by clause. The answer is: never.
– Strawberry
Mar 24 at 6:42
There is no default behaviour here. It's completely arbitrary. You could get the first row every time, then get the second in testing. Don't use this.
– MatBailie
Mar 24 at 8:23
all, please check my updated question. I put my new query inside.
– dataviews
Mar 24 at 15:15
add a comment |
Do tracking numbers always increase?
– Nick
Mar 24 at 4:32
I'm not sure. There are 3 different formats being stored, but I think they are all uniquely created, and not incremental.
– dataviews
Mar 24 at 5:51
3
I guess the question is, in the absence of any aggregating functions, when should you use a group by clause. The answer is: never.
– Strawberry
Mar 24 at 6:42
There is no default behaviour here. It's completely arbitrary. You could get the first row every time, then get the second in testing. Don't use this.
– MatBailie
Mar 24 at 8:23
all, please check my updated question. I put my new query inside.
– dataviews
Mar 24 at 15:15
Do tracking numbers always increase?
– Nick
Mar 24 at 4:32
Do tracking numbers always increase?
– Nick
Mar 24 at 4:32
I'm not sure. There are 3 different formats being stored, but I think they are all uniquely created, and not incremental.
– dataviews
Mar 24 at 5:51
I'm not sure. There are 3 different formats being stored, but I think they are all uniquely created, and not incremental.
– dataviews
Mar 24 at 5:51
3
3
I guess the question is, in the absence of any aggregating functions, when should you use a group by clause. The answer is: never.
– Strawberry
Mar 24 at 6:42
I guess the question is, in the absence of any aggregating functions, when should you use a group by clause. The answer is: never.
– Strawberry
Mar 24 at 6:42
There is no default behaviour here. It's completely arbitrary. You could get the first row every time, then get the second in testing. Don't use this.
– MatBailie
Mar 24 at 8:23
There is no default behaviour here. It's completely arbitrary. You could get the first row every time, then get the second in testing. Don't use this.
– MatBailie
Mar 24 at 8:23
all, please check my updated question. I put my new query inside.
– dataviews
Mar 24 at 15:15
all, please check my updated question. I put my new query inside.
– dataviews
Mar 24 at 15:15
add a comment |
1 Answer
1
active
oldest
votes
Group byis for aggregation function as MIN() ,, MAX() , COUNT() . .. and is for define respect which column the aggreagtion function must be performed..
If you are looking for a specific ordered result you should use ORDER BY that work for the columns value as is
select tablea.order_number, tablea.order_name, tableb.tracking_number
from tablea
join table b on tablea.order_number = tableb.order_number
order by tablea.order_number
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%2f55320711%2fmysql-understanding-group-by-with-a-joined-table%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
Group byis for aggregation function as MIN() ,, MAX() , COUNT() . .. and is for define respect which column the aggreagtion function must be performed..
If you are looking for a specific ordered result you should use ORDER BY that work for the columns value as is
select tablea.order_number, tablea.order_name, tableb.tracking_number
from tablea
join table b on tablea.order_number = tableb.order_number
order by tablea.order_number
add a comment |
Group byis for aggregation function as MIN() ,, MAX() , COUNT() . .. and is for define respect which column the aggreagtion function must be performed..
If you are looking for a specific ordered result you should use ORDER BY that work for the columns value as is
select tablea.order_number, tablea.order_name, tableb.tracking_number
from tablea
join table b on tablea.order_number = tableb.order_number
order by tablea.order_number
add a comment |
Group byis for aggregation function as MIN() ,, MAX() , COUNT() . .. and is for define respect which column the aggreagtion function must be performed..
If you are looking for a specific ordered result you should use ORDER BY that work for the columns value as is
select tablea.order_number, tablea.order_name, tableb.tracking_number
from tablea
join table b on tablea.order_number = tableb.order_number
order by tablea.order_number
Group byis for aggregation function as MIN() ,, MAX() , COUNT() . .. and is for define respect which column the aggreagtion function must be performed..
If you are looking for a specific ordered result you should use ORDER BY that work for the columns value as is
select tablea.order_number, tablea.order_name, tableb.tracking_number
from tablea
join table b on tablea.order_number = tableb.order_number
order by tablea.order_number
answered Mar 24 at 7:53
scaisEdgescaisEdge
101k105472
101k105472
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%2f55320711%2fmysql-understanding-group-by-with-a-joined-table%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
Do tracking numbers always increase?
– Nick
Mar 24 at 4:32
I'm not sure. There are 3 different formats being stored, but I think they are all uniquely created, and not incremental.
– dataviews
Mar 24 at 5:51
3
I guess the question is, in the absence of any aggregating functions, when should you use a group by clause. The answer is: never.
– Strawberry
Mar 24 at 6:42
There is no default behaviour here. It's completely arbitrary. You could get the first row every time, then get the second in testing. Don't use this.
– MatBailie
Mar 24 at 8:23
all, please check my updated question. I put my new query inside.
– dataviews
Mar 24 at 15:15