Get Rows that are not from the logged-in user

Asked

Viewed 209 times

5

I have a table with columns para and de where I enter different Ids in each one, but I need to get the ID of the user who SENT the request to me, ie the ID that is not the same as the logged in user.

SELECT `username` FROM all, users WHERE (`de` = 'ID DO USUARIO LOGADO' OR `para` = 'ID DO USUARIO LOGADO') AND (users.idu = chat.from NOT IN (ID DO USUARIO LOGADO) OR users.idu = chat.to NOT IN (ID DO USUARIO LOGADO)) GROUP BY `c_id` order by id desc

Dai created this code, but it continues to return me the ID of the logged-in user and not of what sent, which occurs?

I used the NOT IN to prevent it from being logged in user ID, it still didn’t work.

Exemplo: 

Tabela ALL

-------------
| de | para |
-------------
|  2 |  1   |
-------------

TABELA USERS

------------------
| idu | username |
------------------
|  1  |  MARCOS  |
------------------
|  2  |  MANUEL  |
------------------

SELECIONA O ROW ONDE (para = USUARIO LOGADO or de = USUARIO LOGADO) e depois vai na tabela USERS e seleciona o usuário que não seja o usuário logado dentre `para` e `de`.

Ou Seja, retornaria -> Manuel
  • What are the structures of your tables? It would be good also some sample data. I have a little difficulty in understanding your query, that column username is on which table?

  • @mgibsonbr Atualizei, it is clear I hope.

1 answer

4

From what I understand, one of the ids (de or para) must be the one of the logged in user, and the other not, and you want the other in either of the two situations, right? There are several ways to do this, but the most semantically correct in my opinion would be using a union:

select username
from users
where idu in (
    select de from `all` where para = 'ID DO USUARIO LOGADO'
    union
    select para from `all` where de = 'ID DO USUARIO LOGADO'
);

Example in Sqlfiddle. That is, select the set where the logged in user is the para, and make the union with the set where the logged in user is the de. In each case, the id that interests you is the other. Search in the table users who has that id.

Updating: an alternative way, without involving subquery, would simply change the order of their conditions; instead of a conjunction of disjunctions (i.e. AND of ORs) you do otherwise. This would require however a join beyond the use of case:

select case
           when u1.idu = 'ID DO USUARIO LOGADO' then u2.username
           else u1.username
       end as username
from users u1
    join `all` a  on u1.idu = a.de
    join users u2 on u2.idu = a.para
where
    (a.de = 'ID DO USUARIO LOGADO' AND a.para != 'ID DO USUARIO LOGADO') OR
    (a.de != 'ID DO USUARIO LOGADO' AND a.para = 'ID DO USUARIO LOGADO');

Example.

  • I must have expressed myself badly, it’s all right except "and the other not, and you want either, right?" is exactly the opposite I wish to pick up the user username opposite to the logged in.

  • @user3163662 And that’s exactly what this code is doing! In the example, the logged-in user is "Manuel", so he returns "Marcos". When I said "either of the two" I referred to either of the two situations: where the logged-in user is the sender and where the logged-in user is the recipient. But what is returned is always the other user, which is not logged in.

Browser other questions tagged

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