Get one row from table based off anothermySQL join same table twiceBest way to get identity of inserted row?How to concatenate text from multiple rows into a single text string in SQL server?Get list of all tables in Oracle?SQL update from one Table to another based on a ID matchSetting up foreign keys in phpMyAdmin?MySQL - UPDATE query based on SELECT QuerySQL Server: How to Join to first rowHow to get the sizes of the tables of a MySQL database?SQL Inner-join with 3 tables?MySQL replace foreign key in result table with data from FK table

How did the SysRq key get onto modern keyboards if it's rarely used?

When I cite content from a book, should I say "section 2.3.2.1 of book... " or "section 2.3.2.1 of `the` book ..."?

Did the Americans trade destroyers in the "destroyer deal" that they would later need themselves?

Must a song using the A minor scale begin or end with an Am chord? If not, how can I tell what the scale is?

Why not notify faculty candidates of the position being filled?

What is "aligned sequences" and "consensus sequence" in the context of sequence logo? How to compute these?

How to find the mass density function of a solid given its total mass, its volume and the position of its center of mass?

Why did House of Representatives need to condemn Trumps Tweets?

Irreducible factors of primitive permutation group representation

What do I do with a party that is much stronger than their level?

World of (nearly) identical snowflakes

What are the closest international airports in different countries?

Composing fill in the blanks

How do you pronounce "Hain"?

Why would anyone ever invest in a cash-only etf?

Why force the nose of 737 Max down in the first place?

Wand of the War Mage spellcasting focus and bonus interaction with multiclassing

If Trump gets impeached, how long would Pence be president?

Why do they sell Cat 5 Ethernet splitters if you can’t split the signal?

Assuring luggage isn't lost with short layover

Finding the Maximum of a Continuous Function over a Closed Interval

Can an Oathbreaker Paladin reform and choose a different Paladin subclass?

Increasing labelling size in a ListPointPlot3D

What do you call a flexible diving platform?



Get one row from table based off another


mySQL join same table twiceBest way to get identity of inserted row?How to concatenate text from multiple rows into a single text string in SQL server?Get list of all tables in Oracle?SQL update from one Table to another based on a ID matchSetting up foreign keys in phpMyAdmin?MySQL - UPDATE query based on SELECT QuerySQL Server: How to Join to first rowHow to get the sizes of the tables of a MySQL database?SQL Inner-join with 3 tables?MySQL replace foreign key in result table with data from FK table






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I have two tables, users and teaching



The users table contains both teachers and students. The teaching table contains the relation between the two.



I want to be able to get the student based off who "teaches" them.



Both teachers and students have an id, and I would have the teachers id to look up in the teaching table to find the students they teach.



Any ideas as to how this is done? I would like it to return the id of the student.



I've tried doing a simple join statement, but this would fetch the teacher, not the student.



Thanks in advance










share|improve this question


























  • Possible duplicate of mySQL join same table twice

    – RisingSun
    Mar 26 at 19:37






  • 1





    Sample data and desired results would really help.

    – Gordon Linoff
    Mar 26 at 19:38

















1















I have two tables, users and teaching



The users table contains both teachers and students. The teaching table contains the relation between the two.



I want to be able to get the student based off who "teaches" them.



Both teachers and students have an id, and I would have the teachers id to look up in the teaching table to find the students they teach.



Any ideas as to how this is done? I would like it to return the id of the student.



I've tried doing a simple join statement, but this would fetch the teacher, not the student.



Thanks in advance










share|improve this question


























  • Possible duplicate of mySQL join same table twice

    – RisingSun
    Mar 26 at 19:37






  • 1





    Sample data and desired results would really help.

    – Gordon Linoff
    Mar 26 at 19:38













1












1








1








I have two tables, users and teaching



The users table contains both teachers and students. The teaching table contains the relation between the two.



I want to be able to get the student based off who "teaches" them.



Both teachers and students have an id, and I would have the teachers id to look up in the teaching table to find the students they teach.



Any ideas as to how this is done? I would like it to return the id of the student.



I've tried doing a simple join statement, but this would fetch the teacher, not the student.



Thanks in advance










share|improve this question
















I have two tables, users and teaching



The users table contains both teachers and students. The teaching table contains the relation between the two.



I want to be able to get the student based off who "teaches" them.



Both teachers and students have an id, and I would have the teachers id to look up in the teaching table to find the students they teach.



Any ideas as to how this is done? I would like it to return the id of the student.



I've tried doing a simple join statement, but this would fetch the teacher, not the student.



Thanks in advance







mysql sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









Dharman

7,4159 gold badges29 silver badges63 bronze badges




7,4159 gold badges29 silver badges63 bronze badges










asked Mar 26 at 19:30







user10976392






















  • Possible duplicate of mySQL join same table twice

    – RisingSun
    Mar 26 at 19:37






  • 1





    Sample data and desired results would really help.

    – Gordon Linoff
    Mar 26 at 19:38

















  • Possible duplicate of mySQL join same table twice

    – RisingSun
    Mar 26 at 19:37






  • 1





    Sample data and desired results would really help.

    – Gordon Linoff
    Mar 26 at 19:38
















Possible duplicate of mySQL join same table twice

– RisingSun
Mar 26 at 19:37





Possible duplicate of mySQL join same table twice

– RisingSun
Mar 26 at 19:37




1




1





Sample data and desired results would really help.

– Gordon Linoff
Mar 26 at 19:38





Sample data and desired results would really help.

– Gordon Linoff
Mar 26 at 19:38












2 Answers
2






active

oldest

votes


















0














Something like that?



SELECT users.id FROM users INNER JOIN teaching on users.id = teaching.userId WHERE teaching.teacherId = '';





share|improve this answer
































    0














    You just need to join the users table again to the teaching table. Just make sure in one join you are using the teachers' ids and in the other the students' ids.



    You can join the same table multiple times and that is totally legal in MySQL






    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%2f55364934%2fget-one-row-from-table-based-off-another%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














      Something like that?



      SELECT users.id FROM users INNER JOIN teaching on users.id = teaching.userId WHERE teaching.teacherId = '';





      share|improve this answer





























        0














        Something like that?



        SELECT users.id FROM users INNER JOIN teaching on users.id = teaching.userId WHERE teaching.teacherId = '';





        share|improve this answer



























          0












          0








          0







          Something like that?



          SELECT users.id FROM users INNER JOIN teaching on users.id = teaching.userId WHERE teaching.teacherId = '';





          share|improve this answer













          Something like that?



          SELECT users.id FROM users INNER JOIN teaching on users.id = teaching.userId WHERE teaching.teacherId = '';






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 26 at 19:33









          Charis MoutafidisCharis Moutafidis

          3184 silver badges14 bronze badges




          3184 silver badges14 bronze badges


























              0














              You just need to join the users table again to the teaching table. Just make sure in one join you are using the teachers' ids and in the other the students' ids.



              You can join the same table multiple times and that is totally legal in MySQL






              share|improve this answer





























                0














                You just need to join the users table again to the teaching table. Just make sure in one join you are using the teachers' ids and in the other the students' ids.



                You can join the same table multiple times and that is totally legal in MySQL






                share|improve this answer



























                  0












                  0








                  0







                  You just need to join the users table again to the teaching table. Just make sure in one join you are using the teachers' ids and in the other the students' ids.



                  You can join the same table multiple times and that is totally legal in MySQL






                  share|improve this answer













                  You just need to join the users table again to the teaching table. Just make sure in one join you are using the teachers' ids and in the other the students' ids.



                  You can join the same table multiple times and that is totally legal in MySQL







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 26 at 19:32









                  RisingSunRisingSun

                  1,22118 silver badges37 bronze badges




                  1,22118 silver badges37 bronze badges






























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55364934%2fget-one-row-from-table-based-off-another%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

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript