L
L
Li2020-04-18 13:17:49
MySQL
Li, 2020-04-18 13:17:49

How to merge three MySQL tables and exclude certain rows?

There are three tables:
1. A table with sciences
2. A table with learned sciences
3. A table indicating which science will open after learning a certain one.

There can be over 500 sciences, so I don’t want to display all the sciences at once, and there’s no point. It is necessary to display only those that can be studied, as well as hide those studied.

In my case, studied Agriculture and Livestock. They opened access for study: Milk, meat, wheel, iron.
Car and Rocket are not available. They need to be hidden.

I've been trying to do it for two days with the help of JOINs. It turns out nonsense. Can you please tell me how this can be implemented?

5e9acde04c40e775464640.png

5e9acde9196b4624944211.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2020-04-18
@cubaPro

What can you do here for two days?

SELECT `s`.*
  FROM `learned_sciences` AS `l`
  JOIN `science_branch` AS `b` ON `b`.`science_id` = `l`.`science_id`
  JOIN `sciences` AS `s` ON `s`.`id` = `b`.`open_science_id`
  WHERE `l``user_id` = :iserId
    AND `s`.`id` NOT IN (
      SELECT `science_id`
        FROM `learned_sciences`
        WHERE `user_id` = :userId
    )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question