Good day! There is such a table:

+----+-----+------+ | id | uid | chat | +----+-----+------+ | 3 | 1 | 2 | | 6 | 1 | 3 | | 1 | 6 | 1 | | 4 | 6 | 2 | | 2 | 9 | 1 | | 5 | 9 | 3 | +----+-----+------+ 

How to choose chat, where uid is 1 and 6? At the same time, another query may be on the sample, where uid is equal to 1,6,9,11;

  • You are in the condition of contradicting yourself. Say that you need to choose where uid is 1 or 6, and at the very end bring "for example 1,6,9,11" Also, it is absolutely not clear how to calculate the number of participants. - KoVadim
  • Not 1 or 6 and 1 And 6. The number of participants is not necessary to count - another request may be on a sample, where uid = 1,6,9,11. - woland
  • then the answer is short - there are no such lines. Since the uid is single in each line, the lines that have uid and 1 and 6 at the same time do not exist. - KoVadim
  • I need to select both lines - the one in which uid = 6 and the one in which uid = 9. And return the chat, which they have the same - woland
  • Show the full result you want. It will be good if you show in the form of a finished structure in your favorite language. - KoVadim

4 answers 4

 SELECT * from `table` where `uid` = 1 OR `uid` = 6 
  • It is written in Russian in white -> uid should be equal to 1 and 6. Before such a sample I would have guessed. - woland
  • one
    @woland do you yourself understand what you want? There are no such records and can not be. - BuilderC
  • you need to select 2 entries - where uid = 6 and uid = 1. The meaning of the request is to get the chat field that will be the same for these two lines - woland

What is difficult? We select all chats for one uid, all chats for the second uid, we compare. Voila!

 +------+ | chat | +------+ | 2 | +------+ 

Is the idea clear?

    If you get the idea right, a combined query is used here. In this case, I met a record through UNION:

     SELECT * FROM `table` WHERE `uid` = 1 UNION SELECT * FROM `table` WHERE `uid` = 6 
    • In this query, the union will work as in. This does not fit - woland

    Maybe I misunderstood something, but what is so bad about this query:

     SELECT `chat` FROM `yourTable` WHERE `uid` = 1 AND `uid` = 6 

    Tears out from the table all the chat fields, uid for which 1 and 6:

     +------+ | chat| +------+ | 2 | | 3 | | 1 | | 2 | +------+ 

    Corresponds to the condition of the problem.