Get the data out of the two tables with one request. MySql



  • There are two tables - defCategories and userCategories♪ There's no connection between them, they're different. ♪ defCategories Two pillars - id and defCategory♪ ♪ userCategories Three pillars - iduser_id and userCategory♪ How to pull out all categories defCategories and categories only to be specified user_id of userCategories?

    1 - defCategories

    id |  defCategories  |
    1  |      Sport      |
    2  |   Programming   |
    3  |     Animals     |
    

    2 - userCategories

    id | user_id |  userCategory
    1  |    1    |  Work
    2  |    7    |  Home
    3  |    3    |  Other
    4  |    20   |  Food
    

    I'd like to get all the records on the way out. defCategories and specific user records userCategories One request.

    3 - Result (expected result)

    | defCategories |  userCategory
    |     Sport     |  Work
    |  Programming  |  Home
    |    Animals    |  Other
    |               |  Food
    

    Like this.



  • (1) select defCategory from defCategories

    2) select userCategory from userCategories where user_id = bunch you want




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2