There are two tables - defCategories and userCategories . There are no connections between them, they are different. In defCategories two columns are id and defCategory . There userCategories three columns in userCategoriesid , user_id and userCategory . How to pull out all categories from defCategories and categories only to a specific user by user_id from 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 

At the exit, I would like to get all the records from defCategories and the records of a specific user from userCategories one request.

3 - Result (Expected Result)

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

Approximately in this form.

  • I didn't understand the question - HELO WORD
  • Always bring in similar questions sample data in the input tables and what data you expect at the output. For nothing is clear. If the tables have nothing in common, then how do you expect this unrelated data to be placed at the output. Maybe you just need a union, and maybe join ... - Mike
  • @Mike added examples to the header. I tried Union, it gives me everything in one, not separately - tobish it turns out this: Sport Programming also does not work with JOIN. It turns out this way (Everything in one line turned out. Where | | is a new line.) | Sport | Work | | Sport | Home | | Sport | Other | | Sport | Food | | Programming | Work | | Programming | Home | | Programming | Other | | Programming | Food | | Sport | Work | | Sport | Home | | Sport | Other | | Sport | Food | | Programming | Work | | Programming | Home | | Programming | Other | | Programming | Food | etc .. - Tsyklop
  • And what would you like to see at the exit. union seems the only thing that could be applied. Just need to clarify what is "issue separately" - Mike
  • one
    And why do you need to receive absolutely unrelated data in one request? speed it does not add. It is not convenient to work in one cycle with data of different types. You can of course make select NULL as defCategories, userCategory from userCategories union select defCategories, NULL from defCategories though the records will be strictly separate lines, in one line Sport | Work will not be. And what would you like right here ... you can of course do it, believe me, it’s not worth it, the request will be spooky - Mike

1 answer 1

1) select defCategory from defCategories

2) select userCategory from userCategories where user_id = <what you want>

  • I would like one request. - Tsyklop
  • I would like one request that you very vaguely state what you want to receive. But here, as I understand it, the principle of all honest traders should act: “The client should get what he NEED, not what he WANTS. So, from my point of view, you NEED: select userCategor, defCategory from userCategories, defCategories where user_id = defCategories.id This is what intuition tells me ... Although I may be wrong ... - Sergey
  • Added to the cap what you need. - Tsyklop
  • the author of the question actually does just that and the SQL variant of the query that I suggested to you will form such an answer, except for the last line: - Sergey
  • This is the line in your example that contains: Food - Sergey