Order By same column using joined table Mysql












0














I have two tables: mother and child. Each mother has one or more children.



mother:



id | name  | age 
1 | name1 | 30
2 | name2 | 40
3 | name3 | 35


child:



id | name   | age | id_mother
1 | child1 | 15 | 1
2 | child2 | 10 | 2


The result that I want is order mother order by age ASC and order by their children age asc



Edit (from comments): I simply want to order by minimum child age (if child exists), and then by mother age.



For example :



id | name  | age 
2 | name2 | 40
1 | name1 | 30
3 | name3 | 35


I tried this sql:



SELECT * 
FROM mother m
ORDER BY m.age asc , (SELECT MIN(age) FROM child c WHERE c.id_mother == m.id)


but it give just mothers ordered by their age?










share|improve this question




















  • 1




    I don't understand your data or the expected output. You might want to clean up your question a bit.
    – Tim Biegeleisen
    Nov 16 at 11:11










  • age column in your expected output does not make sense.
    – Madhur Bhaiya
    Nov 16 at 11:13










  • age to show you why I want this order
    – Sabra
    Nov 16 at 11:15










  • Why isn't mother with name1 not coming at the top ?
    – Madhur Bhaiya
    Nov 16 at 11:16






  • 1




    I think you simply want to order by minimum child age (if child exists), and then by mother age ?
    – Madhur Bhaiya
    Nov 16 at 11:21
















0














I have two tables: mother and child. Each mother has one or more children.



mother:



id | name  | age 
1 | name1 | 30
2 | name2 | 40
3 | name3 | 35


child:



id | name   | age | id_mother
1 | child1 | 15 | 1
2 | child2 | 10 | 2


The result that I want is order mother order by age ASC and order by their children age asc



Edit (from comments): I simply want to order by minimum child age (if child exists), and then by mother age.



For example :



id | name  | age 
2 | name2 | 40
1 | name1 | 30
3 | name3 | 35


I tried this sql:



SELECT * 
FROM mother m
ORDER BY m.age asc , (SELECT MIN(age) FROM child c WHERE c.id_mother == m.id)


but it give just mothers ordered by their age?










share|improve this question




















  • 1




    I don't understand your data or the expected output. You might want to clean up your question a bit.
    – Tim Biegeleisen
    Nov 16 at 11:11










  • age column in your expected output does not make sense.
    – Madhur Bhaiya
    Nov 16 at 11:13










  • age to show you why I want this order
    – Sabra
    Nov 16 at 11:15










  • Why isn't mother with name1 not coming at the top ?
    – Madhur Bhaiya
    Nov 16 at 11:16






  • 1




    I think you simply want to order by minimum child age (if child exists), and then by mother age ?
    – Madhur Bhaiya
    Nov 16 at 11:21














0












0








0







I have two tables: mother and child. Each mother has one or more children.



mother:



id | name  | age 
1 | name1 | 30
2 | name2 | 40
3 | name3 | 35


child:



id | name   | age | id_mother
1 | child1 | 15 | 1
2 | child2 | 10 | 2


The result that I want is order mother order by age ASC and order by their children age asc



Edit (from comments): I simply want to order by minimum child age (if child exists), and then by mother age.



For example :



id | name  | age 
2 | name2 | 40
1 | name1 | 30
3 | name3 | 35


I tried this sql:



SELECT * 
FROM mother m
ORDER BY m.age asc , (SELECT MIN(age) FROM child c WHERE c.id_mother == m.id)


but it give just mothers ordered by their age?










share|improve this question















I have two tables: mother and child. Each mother has one or more children.



mother:



id | name  | age 
1 | name1 | 30
2 | name2 | 40
3 | name3 | 35


child:



id | name   | age | id_mother
1 | child1 | 15 | 1
2 | child2 | 10 | 2


The result that I want is order mother order by age ASC and order by their children age asc



Edit (from comments): I simply want to order by minimum child age (if child exists), and then by mother age.



For example :



id | name  | age 
2 | name2 | 40
1 | name1 | 30
3 | name3 | 35


I tried this sql:



SELECT * 
FROM mother m
ORDER BY m.age asc , (SELECT MIN(age) FROM child c WHERE c.id_mother == m.id)


but it give just mothers ordered by their age?







mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 at 11:28









Madhur Bhaiya

19.5k62236




19.5k62236










asked Nov 16 at 11:09









Sabra

3911




3911








  • 1




    I don't understand your data or the expected output. You might want to clean up your question a bit.
    – Tim Biegeleisen
    Nov 16 at 11:11










  • age column in your expected output does not make sense.
    – Madhur Bhaiya
    Nov 16 at 11:13










  • age to show you why I want this order
    – Sabra
    Nov 16 at 11:15










  • Why isn't mother with name1 not coming at the top ?
    – Madhur Bhaiya
    Nov 16 at 11:16






  • 1




    I think you simply want to order by minimum child age (if child exists), and then by mother age ?
    – Madhur Bhaiya
    Nov 16 at 11:21














  • 1




    I don't understand your data or the expected output. You might want to clean up your question a bit.
    – Tim Biegeleisen
    Nov 16 at 11:11










  • age column in your expected output does not make sense.
    – Madhur Bhaiya
    Nov 16 at 11:13










  • age to show you why I want this order
    – Sabra
    Nov 16 at 11:15










  • Why isn't mother with name1 not coming at the top ?
    – Madhur Bhaiya
    Nov 16 at 11:16






  • 1




    I think you simply want to order by minimum child age (if child exists), and then by mother age ?
    – Madhur Bhaiya
    Nov 16 at 11:21








1




1




I don't understand your data or the expected output. You might want to clean up your question a bit.
– Tim Biegeleisen
Nov 16 at 11:11




I don't understand your data or the expected output. You might want to clean up your question a bit.
– Tim Biegeleisen
Nov 16 at 11:11












age column in your expected output does not make sense.
– Madhur Bhaiya
Nov 16 at 11:13




age column in your expected output does not make sense.
– Madhur Bhaiya
Nov 16 at 11:13












age to show you why I want this order
– Sabra
Nov 16 at 11:15




age to show you why I want this order
– Sabra
Nov 16 at 11:15












Why isn't mother with name1 not coming at the top ?
– Madhur Bhaiya
Nov 16 at 11:16




Why isn't mother with name1 not coming at the top ?
– Madhur Bhaiya
Nov 16 at 11:16




1




1




I think you simply want to order by minimum child age (if child exists), and then by mother age ?
– Madhur Bhaiya
Nov 16 at 11:21




I think you simply want to order by minimum child age (if child exists), and then by mother age ?
– Madhur Bhaiya
Nov 16 at 11:21












2 Answers
2






active

oldest

votes


















0














I think you simply want to order by minimum child age (if child exists), and then by mother age.




  • In a Correlated Subquery, you can get the minimum age out of the mother's children (if they exist).

  • In ORDER BY, we will use IS NOT NULL to put the cases first in ordering where a minimum child age value exists.


Try:



SELECT
m.*,
(SELECT MIN(c.age)
FROM child AS c
WHERE c.id_mother = m.id) AS min_child_age
FROM mother AS m
ORDER BY
min_child_age IS NOT NULL DESC,
min_child_age ASC,
m.age ASC




Schema (MySQL v5.7)



create table mother 
(id int, name varchar(8), age int);

insert into mother values
(1, 'name1', 30), (2, 'name2', 40), (3, 'name3', 35);


create table child
(id int , name varchar(8), age int, id_mother int);

insert into child values
(1 , 'child1' , 15 , 1 ),
(2 , 'child2' , 10 , 2);




Result



| id  | name  | age | min_child_age |
| --- | ----- | --- | ------------- |
| 2 | name2 | 40 | 10 |
| 1 | name1 | 30 | 15 |
| 3 | name3 | 35 | |




View on DB Fiddle






share|improve this answer































    0














    try using this method :
    select * from mother;
    +----+-------+-----+
    | id | name | age |
    +----+-------+-----+
    | 1 | name1 | 30 |
    | 2 | name2 | 40 |
    | 3 | name3 | 35 |
    +----+-------+-----+

    select * from child;
    +----+--------+-----+-----------+
    | id | name | age | id_mother |
    +----+--------+-----+-----------+
    | 1 | child1 | 15 | 1 |
    | 2 | child2 | 10 | 2 |
    +----+--------+-----+-----------+


    SELECT mo1. * , (SELECT MIN( ch1.age ) FROM child AS ch1 WHERE ch1.id_mother = mo1.id) AS child_age FROM mother AS mo1 ORDER BY child_age IS NOT NULL DESC , child_age ASC ;
    +----+-------+-----+-----------+
    | id | name | age | child_age |
    +----+-------+-----+-----------+
    | 2 | name2 | 40 | 10 |
    | 1 | name1 | 30 | 15 |
    | 3 | name3 | 35 | NULL |
    +----+-------+-----+-----------+





    share|improve this answer





















      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
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336680%2forder-by-same-column-using-joined-table-mysql%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









      0














      I think you simply want to order by minimum child age (if child exists), and then by mother age.




      • In a Correlated Subquery, you can get the minimum age out of the mother's children (if they exist).

      • In ORDER BY, we will use IS NOT NULL to put the cases first in ordering where a minimum child age value exists.


      Try:



      SELECT
      m.*,
      (SELECT MIN(c.age)
      FROM child AS c
      WHERE c.id_mother = m.id) AS min_child_age
      FROM mother AS m
      ORDER BY
      min_child_age IS NOT NULL DESC,
      min_child_age ASC,
      m.age ASC




      Schema (MySQL v5.7)



      create table mother 
      (id int, name varchar(8), age int);

      insert into mother values
      (1, 'name1', 30), (2, 'name2', 40), (3, 'name3', 35);


      create table child
      (id int , name varchar(8), age int, id_mother int);

      insert into child values
      (1 , 'child1' , 15 , 1 ),
      (2 , 'child2' , 10 , 2);




      Result



      | id  | name  | age | min_child_age |
      | --- | ----- | --- | ------------- |
      | 2 | name2 | 40 | 10 |
      | 1 | name1 | 30 | 15 |
      | 3 | name3 | 35 | |




      View on DB Fiddle






      share|improve this answer




























        0














        I think you simply want to order by minimum child age (if child exists), and then by mother age.




        • In a Correlated Subquery, you can get the minimum age out of the mother's children (if they exist).

        • In ORDER BY, we will use IS NOT NULL to put the cases first in ordering where a minimum child age value exists.


        Try:



        SELECT
        m.*,
        (SELECT MIN(c.age)
        FROM child AS c
        WHERE c.id_mother = m.id) AS min_child_age
        FROM mother AS m
        ORDER BY
        min_child_age IS NOT NULL DESC,
        min_child_age ASC,
        m.age ASC




        Schema (MySQL v5.7)



        create table mother 
        (id int, name varchar(8), age int);

        insert into mother values
        (1, 'name1', 30), (2, 'name2', 40), (3, 'name3', 35);


        create table child
        (id int , name varchar(8), age int, id_mother int);

        insert into child values
        (1 , 'child1' , 15 , 1 ),
        (2 , 'child2' , 10 , 2);




        Result



        | id  | name  | age | min_child_age |
        | --- | ----- | --- | ------------- |
        | 2 | name2 | 40 | 10 |
        | 1 | name1 | 30 | 15 |
        | 3 | name3 | 35 | |




        View on DB Fiddle






        share|improve this answer


























          0












          0








          0






          I think you simply want to order by minimum child age (if child exists), and then by mother age.




          • In a Correlated Subquery, you can get the minimum age out of the mother's children (if they exist).

          • In ORDER BY, we will use IS NOT NULL to put the cases first in ordering where a minimum child age value exists.


          Try:



          SELECT
          m.*,
          (SELECT MIN(c.age)
          FROM child AS c
          WHERE c.id_mother = m.id) AS min_child_age
          FROM mother AS m
          ORDER BY
          min_child_age IS NOT NULL DESC,
          min_child_age ASC,
          m.age ASC




          Schema (MySQL v5.7)



          create table mother 
          (id int, name varchar(8), age int);

          insert into mother values
          (1, 'name1', 30), (2, 'name2', 40), (3, 'name3', 35);


          create table child
          (id int , name varchar(8), age int, id_mother int);

          insert into child values
          (1 , 'child1' , 15 , 1 ),
          (2 , 'child2' , 10 , 2);




          Result



          | id  | name  | age | min_child_age |
          | --- | ----- | --- | ------------- |
          | 2 | name2 | 40 | 10 |
          | 1 | name1 | 30 | 15 |
          | 3 | name3 | 35 | |




          View on DB Fiddle






          share|improve this answer














          I think you simply want to order by minimum child age (if child exists), and then by mother age.




          • In a Correlated Subquery, you can get the minimum age out of the mother's children (if they exist).

          • In ORDER BY, we will use IS NOT NULL to put the cases first in ordering where a minimum child age value exists.


          Try:



          SELECT
          m.*,
          (SELECT MIN(c.age)
          FROM child AS c
          WHERE c.id_mother = m.id) AS min_child_age
          FROM mother AS m
          ORDER BY
          min_child_age IS NOT NULL DESC,
          min_child_age ASC,
          m.age ASC




          Schema (MySQL v5.7)



          create table mother 
          (id int, name varchar(8), age int);

          insert into mother values
          (1, 'name1', 30), (2, 'name2', 40), (3, 'name3', 35);


          create table child
          (id int , name varchar(8), age int, id_mother int);

          insert into child values
          (1 , 'child1' , 15 , 1 ),
          (2 , 'child2' , 10 , 2);




          Result



          | id  | name  | age | min_child_age |
          | --- | ----- | --- | ------------- |
          | 2 | name2 | 40 | 10 |
          | 1 | name1 | 30 | 15 |
          | 3 | name3 | 35 | |




          View on DB Fiddle







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 16 at 11:37

























          answered Nov 16 at 11:27









          Madhur Bhaiya

          19.5k62236




          19.5k62236

























              0














              try using this method :
              select * from mother;
              +----+-------+-----+
              | id | name | age |
              +----+-------+-----+
              | 1 | name1 | 30 |
              | 2 | name2 | 40 |
              | 3 | name3 | 35 |
              +----+-------+-----+

              select * from child;
              +----+--------+-----+-----------+
              | id | name | age | id_mother |
              +----+--------+-----+-----------+
              | 1 | child1 | 15 | 1 |
              | 2 | child2 | 10 | 2 |
              +----+--------+-----+-----------+


              SELECT mo1. * , (SELECT MIN( ch1.age ) FROM child AS ch1 WHERE ch1.id_mother = mo1.id) AS child_age FROM mother AS mo1 ORDER BY child_age IS NOT NULL DESC , child_age ASC ;
              +----+-------+-----+-----------+
              | id | name | age | child_age |
              +----+-------+-----+-----------+
              | 2 | name2 | 40 | 10 |
              | 1 | name1 | 30 | 15 |
              | 3 | name3 | 35 | NULL |
              +----+-------+-----+-----------+





              share|improve this answer


























                0














                try using this method :
                select * from mother;
                +----+-------+-----+
                | id | name | age |
                +----+-------+-----+
                | 1 | name1 | 30 |
                | 2 | name2 | 40 |
                | 3 | name3 | 35 |
                +----+-------+-----+

                select * from child;
                +----+--------+-----+-----------+
                | id | name | age | id_mother |
                +----+--------+-----+-----------+
                | 1 | child1 | 15 | 1 |
                | 2 | child2 | 10 | 2 |
                +----+--------+-----+-----------+


                SELECT mo1. * , (SELECT MIN( ch1.age ) FROM child AS ch1 WHERE ch1.id_mother = mo1.id) AS child_age FROM mother AS mo1 ORDER BY child_age IS NOT NULL DESC , child_age ASC ;
                +----+-------+-----+-----------+
                | id | name | age | child_age |
                +----+-------+-----+-----------+
                | 2 | name2 | 40 | 10 |
                | 1 | name1 | 30 | 15 |
                | 3 | name3 | 35 | NULL |
                +----+-------+-----+-----------+





                share|improve this answer
























                  0












                  0








                  0






                  try using this method :
                  select * from mother;
                  +----+-------+-----+
                  | id | name | age |
                  +----+-------+-----+
                  | 1 | name1 | 30 |
                  | 2 | name2 | 40 |
                  | 3 | name3 | 35 |
                  +----+-------+-----+

                  select * from child;
                  +----+--------+-----+-----------+
                  | id | name | age | id_mother |
                  +----+--------+-----+-----------+
                  | 1 | child1 | 15 | 1 |
                  | 2 | child2 | 10 | 2 |
                  +----+--------+-----+-----------+


                  SELECT mo1. * , (SELECT MIN( ch1.age ) FROM child AS ch1 WHERE ch1.id_mother = mo1.id) AS child_age FROM mother AS mo1 ORDER BY child_age IS NOT NULL DESC , child_age ASC ;
                  +----+-------+-----+-----------+
                  | id | name | age | child_age |
                  +----+-------+-----+-----------+
                  | 2 | name2 | 40 | 10 |
                  | 1 | name1 | 30 | 15 |
                  | 3 | name3 | 35 | NULL |
                  +----+-------+-----+-----------+





                  share|improve this answer












                  try using this method :
                  select * from mother;
                  +----+-------+-----+
                  | id | name | age |
                  +----+-------+-----+
                  | 1 | name1 | 30 |
                  | 2 | name2 | 40 |
                  | 3 | name3 | 35 |
                  +----+-------+-----+

                  select * from child;
                  +----+--------+-----+-----------+
                  | id | name | age | id_mother |
                  +----+--------+-----+-----------+
                  | 1 | child1 | 15 | 1 |
                  | 2 | child2 | 10 | 2 |
                  +----+--------+-----+-----------+


                  SELECT mo1. * , (SELECT MIN( ch1.age ) FROM child AS ch1 WHERE ch1.id_mother = mo1.id) AS child_age FROM mother AS mo1 ORDER BY child_age IS NOT NULL DESC , child_age ASC ;
                  +----+-------+-----+-----------+
                  | id | name | age | child_age |
                  +----+-------+-----+-----------+
                  | 2 | name2 | 40 | 10 |
                  | 1 | name1 | 30 | 15 |
                  | 3 | name3 | 35 | NULL |
                  +----+-------+-----+-----------+






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 at 12:01









                  Atul Akabari

                  954




                  954






























                      draft saved

                      draft discarded




















































                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336680%2forder-by-same-column-using-joined-table-mysql%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







                      Popular posts from this blog

                      Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                      ComboBox Display Member on multiple fields

                      Is it possible to collect Nectar points via Trainline?