Problem with SQL return

Asked

Viewed 28 times

-1

I have a table that connects two other tables. "Request" it links "Customers" and "Solitation_description"

It has the cliente_id link columns and request_descricao_id, I need to return clients who have more than one specific request from the table as shown in the figure below.

Demonstrando Tabela

I need to create an sql that I put "Where requecao_descricao_id = 19 and requeca_descricao_id = 8 and requecao_descricao_id = 13" and return this client 1. But don’t know how to do anyone can help me? Because I want customers who have these 3 requests together. If I use the "OR" returns me several that I don’t need and AND returns nothing!

  • You want to make the customer 1 (cliente_id) without filtering it, filtering only by solicitacoes_descricao_id?

  • With a group by us cliente_id and only filtering the request

  • How is your query today? How you tried to do?

  • I made the junctions of the tables and gave a Where with the filters separated by "AND" but then it does not return anything.. pq is not in msm line. I have no idea how to do.

  • It is impossible that the field solicitacao_descricao_id contains 3 distinct values simultaneously (19, 8 and 13). You may want to use OR and not AND.

1 answer

0

Considering the structure of your table, to know the Ids of clients that appear more than once in your table "request", you could do as follows

select cliente_id from solicitacao group by cliente_id HAVING count(*) > 1

You can use this query as a basis to return client names in this situation by doing so:

select * from clientes where id in (select cliente_id from solicitacao group by cliente_id HAVING count(*) > 1)

If you want to see customers who appear more than once and your requests, you can try to do so:

select * from clientes, solicitacao where id = cliente_id and id in (select cliente_id from solicitacao group by cliente_id HAVING count(*) > 1)

Browser other questions tagged

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