Difficulty in formulating query

Asked

Viewed 55 times

0

In a system there are plots registrations . The users can classificar as plots for familyDeParcela each of which user has its own familyDeParcela.

As plots are common to all.

I have 4 tables:

User that has cod_usuario PK
Parcel that has cod_parcela PK
Familiadeparcelas that has cod_usuario FK and cod_familia PK
Parcel that has cod_familiaFK , cod_parcela FK , and a PK composed of cod_familia and cod_parcela

Example of use :

We have 5 plots registered

    Horas extras além da 6a,
    Horas extras além da 8a,
    danos morais,
    danos materiais,
    periculosidade.

User 1 registers the families HORAS EXTRAS, DANOS and OUTROS that will belong only to him and then can classify

    Horas extras além da 6a --> HORAS EXTRAS 
    Horas extras além da 8a --> HORAS EXTRAS
    danos morais ---> DANOS
    danos materiais ---> DANOS
    periculosidade ----> OUTROS

And user 2 registers the families SALARIAL and INDENIZATÓRIA that will belong only to him and then can classify

    Horas extras além da 6a --> SALARIAL
    Horas extras além da 8a --> SALARIAL
    danos morais ---> INDENIZATÓRIA
    danos materiais ---> INDENIZATÓRIA
    periculosidade ----> NULL ( DEIXAR SEM CLASSIFICAR ) 

When a user is using accurate listar all the plots with your Familiadeparcela and if it has not been classified , return only the data from installment, but only that user !

If I do

     select * 
     from Parcela p 
     left outer join Parcela_FamiliaDeParcelas pf ON pf.cod_parcela = p.cod_parcela 
     left outer join FamiliaDeParcelas f on f.cod_familia = pf.cod_familia and f.cod_usuario = @codusuario

With this query for each classification user makes it returns 1 extra occurrence for each classification of the same portion as another user did ( but with null in the data of family )

Add at the end a where f.cod_usuario = @codusuario he returns only the plots of user , but not the plots unclassified

What should I do ?

3 answers

1

The question is very confusing (rs), but from what I understand, I think there is an error of cardinality. See in the image below the relational model of the tables:

inserir a descrição da imagem aqui

If you are correct, you will record the field cod_familia on the table Parcela as a FK (Foreign Key), so there will be no need for the table Parcela_FamiliaParcela. And will also record the field cod_usuario on the table Parcela like a FK.

And your consultation can stay like this:

select *
from parcela p
left join FamiliaDeParcelas fp on (fp.cod_familia = p.cod_familia)
inner join Usuario u on (pf.cod_usuario = u.cod_usuario)
where pf.cod_usuario = @cod_usuario

I hope I’ve cooperated!

  • The diagram you made is correct Eduardo, but a portion does not belong to a user and so I can not put in parcel a FK_USUARIO . Nor can I put Fk_familia in the plot since it does not belong to a user and each user can make their own rating. If I did that I would have to re-register all installments for all users . ex installation1 , usuario1 ,familia1 / installation1 , usuario2,familia2

  • I will try to clarify here , let’s consider that Parcel = Product , Familiadeparcela = Category in a system that all users can sell Rice , chocolate and soda . a user can create a food category and classify Rice, Chocolate and Soda as food. a second user creates 3 categories food, sweet and drink , and classifies rice as food , chocolate as sweet and soda as drink .

  • Eduardo, I managed to solve using right Join ! Thank you so much for the help, I was extremely happy to receive your reply with the work of making the diagram and everything. Soon I will delete this question because I think it does not help many people and really confused . But not before thanking . Brigadesso !!

  • Kkkkk. From your first comment what I was going to actually wouldn’t work. But I’m glad you did. We’re here for that, to try to help. Be at peace.

  • It was the first time I had to wear right Join in my life! I had to start the query from Familiadeparcela to then do the Join in the union table using Familiadeparcela.id and not Parcel.id and how I had to list all the plots right Join Parcel at the end , done this worked right ! the//

0


The problem was this:

How I started the query from Parcel p and made a left Outer Join Parcel using p.cod_parcela the query returned all the records they had p.cod_parcela including those of other users .

I realized this and then started the query from Familiadeparcela Fp and made the one left Join using fp.cod_familia and @codusuario followed by a right Join to list all the plots and so it worked well !

The final query was:

    SELECT *  FROM FamiliaDeParcelas 
    LEFT OUTER JOIN PARCELA_FAMILIAPARCELAS f ON f.cod_familia = FamiliaDeParcelas.cod_familia 
    AND FamiliaDeParcelas.cod_usuario = @codusuario 
    RIGHT OUTER JOIN Parcela ON f.cod_parcela = Parcela.cod_parcela

0

See if this solves your problem:

select * from `Parcela_FamiliaParcela` `pfp`
left join `FamiliaDeParcelas` `fdp` on (`fdp`.`cod_familia`=`pfp`.`cod_familia`) 
left join `Usuario` `u` on(`u`.`cod_usuario`=`fdp`.`cod_usuario`)
left join `Parcela` `p` on (`p`.`cod_parcela`=`pfp`.`cod_parcela`)
where `u`.`cod_usuario`=@cod_user
  • Ivan , I believe that this will only return the portions that were classified , moreover it would not have the need to join the user table since the user data would only come when u.cod_usuario and fdp.cod_usuario were equal , logo Where fdp.cod_usuario = @cod_user would give in it

  • The query I made is to bring everything in the user-based relationship (you can take the condition where. And everything you don’t need). What you believe will only be true if you run the query first, then draw your conclusions. 'Cause I believe you’re wrong.

  • The "left Join" union is not limiting.

  • https://uploaddeimagens.com.br/imagens/uniao-png--2 Follow your query executed, happened exactly what I said... I have 109 plots registered , so listed the 2 classified for this user

  • by the image anyone can see that is not my query... sorry.

  • no, my query, has : * is not picking up 2 fields. Also, the image is not complete. So it doesn’t prove the facts, besides, it’s not about humility, it’s about being clear and transparent. I don’t see it in the way you express yourself. I didn’t want to offend you, only that the lack of humility came from you, when you prejudged my query, because here we are committed to help the other.

  • If my opinion does not meet its purpose, there is no reason to question it, simply discard it. Just as I do when I do not consider it useful. I do not waste my time. So walks the Humility.

  • Ivan , follows his query executed next to the query I used to solve the problem. Note that the number of selected fields does not interfere with the return . https://uploaddeimagens.com.br/imagens/imag-png

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.