I
I
ivan8m82015-12-07 23:56:44
MySQL
ivan8m8, 2015-12-07 23:56:44

How to validate two fields?

DB: sqlfiddle.com/#!9/0f9a0/1
Task: find prisoners who are in the same cell and are on the same case.
I was only able to implement the search for prisoners who are involved in one case.

SELECT K.*
FROM Klienti AS K
WHERE K.k_nomer_dela IN (
  SELECT Kk.k_nomer_dela
  FROM Klienti AS Kk
  GROUP BY Kk.k_nomer_dela
  HAVING COUNT(*) > 1
)

Separately, in a similar way, I can implement a search for prisoners who are sitting in the same cell.
SELECT K.*
FROM Klienti AS K
WHERE K.nomer_kameri IN (
  SELECT Kk.nomer_kameri
  FROM Klienti AS Kk
  GROUP BY Kk.nomer_kameri
  HAVING COUNT(*) > 1
)

But I can’t do it within the framework of one request ...
In fact, I need to combine these two requests together, but, unfortunately, nothing comes out for me for the second day :(
Please help.
The fields for which the selection is made are in the Klienti Table and are called k_nomer_dela, nomer_kameri
Thank you in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nelson, 2015-12-08
@ivan8m8

What if we just put both conditions in the same query and AND between them?

SELECT K.*
FROM Klienti AS K
WHERE K.k_nomer_dela IN (
  SELECT Kk.k_nomer_dela
  FROM Klienti AS Kk
  GROUP BY Kk.k_nomer_dela
  HAVING COUNT(*) > 1
)
AND 
K.nomer_kameri IN (
  SELECT Kk.nomer_kameri
  FROM Klienti AS Kk
  GROUP BY Kk.nomer_kameri
  HAVING COUNT(*) > 1
)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question