Display data from one database depending on values in another table

0

I have the following DB with user, password and if they have different OS (with a boolean for each OS):

I want to create another table in which there is a news record and the OS to which they belong. A story may belong to two OS.

The question is: What would be the easiest way to show the news depending on the OS of the active user in that session?

    
asked by Alejandro 20.03.2018 в 20:16
source

1 answer

1

Here are several issues, the first thing you have to do is rethink the design of the tables and their relationships, as I understood you want the following:

A user can have 1 or many operating systems and an operating system can be related to 1 or many users. (relation m x n)

A news story can have 1 or many (includes 2) operating systems and an operating system can have 1 or many news (m x n ratio).

User Table: id, name, password, etc.

Table of Operating Systems: id, name, acronym (whatever is needed)

Table News: id, title, text, etc.

and we have left pending the relations that by rule and normalizing them if it is of type n x m, implies an intermediate table that contains the ids of the related tables for example:

User: id (+)

UserOperatingSystem: user_id (+) and operating_id system (*)

SistemaOperativo: id (*)

and the same for the other relationship.

If you need to see the news filtered by the operating systems that the user has selected, the query would be something like that, and without giving it much thought:

Select * from Noticia n 
join NoticiaSistemaOperativo nso on n.id = nso.noticia_id
where nso.sistema_operativo_id in (
select uso.sistema_operativo_id from Usuario u 
join  UsuarioSistemaOperativo uso on u.id = uso.usuario_id
where u.id = XXXX
)
    
answered by 21.03.2018 в 02:40