1

I wonder if someone could help me with an SQL query.

I am trying to display all results except where in Table 2 both the userid column equals 1 and the hidden column equals 1.

I am basically trying to hide results from individual users based on their id and the value in the hidden column either 1 or empty.

I have so far managed to make a query that does the opposite and can't figure out how to change it. I have tried adding !=, <>, NOT and a few other things, but nothing is working for me!

Here is the query I am working with.

$stmt = $conn->prepare("SELECT tl.id, tl.name, tl.locale, uh.hidden 
  FROM theList AS tl 
  LEFT JOIN user_hidden_list AS uh ON uh.est_id = tl.id 
  WHERE uh.userid = '1' AND uh.hidden = '1'");

How do I display all results expect those with a uh.userid = 1 and uh.hidden = 1

UPDATE: Tables

Table: user_hidden_list

 userid | Hidden | est_id
---------------------------
   1    | 1      |   1
   2    | 1      |   1
   1    | 1      |   2

Table: theList

id |    name  | locale
------------------------
 1 |    Jacks   |   LDN
 2 |    MacD's  |   LDN
 3 |    BK      |   LDN
 4 |    Byron   |   LDN

So if I am logged in with and userid of:

1 I should see: BK and Byron.

2 I should see: MacD's, BK and Byron.

3 (or anything else) I should see: Jacks, MacD's, BK and Byron.

tbowden
  • 1,008
  • 1
  • 19
  • 44
  • 1
    It should work fine with `!=` or `<>`. Maybe look at [when to use quotes](https://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-back-ticks-in-mysql)? Probably also want to look at proper escaping / [PDO](https://stackoverflow.com/questions/13569/mysqli-or-pdo-what-are-the-pros-and-cons). – ficuscr Oct 16 '18 at 20:52
  • Hint: When you reference a column from an outer-joined table, it turns it into an inner join. You can apply filters in the `ON` portion of your query for your outer-joined table. – RToyo Oct 16 '18 at 20:53
  • What is the column type for userid and hidden? – Danial Wayne Oct 16 '18 at 20:53
  • 1
    Sample data and desired results would really help. – Gordon Linoff Oct 16 '18 at 20:53
  • Thanks everyone! I have posted an update to the question with tables and outcome! – tbowden Oct 16 '18 at 21:06
  • @GordonLinoff Added some sample data! – tbowden Oct 16 '18 at 21:23

3 Answers3

1

If i understood correctly, the table user_hidden_list mantains a relation between one user and the users he can't see. So for, example, user with id equal to 1 (matching on column userid) can't see users 1 and 2 (matching on column est_id).

So, for a particular user with ID = X, we can get the ID list of user he can't see like next:

SELECT
    est_id
FROM
    user_hidden_list
WHERE
    userid = X AND hidden = 1;

Using the previous query, we can get the visible users for user X like on next query:

SELECT
    tl.id, tl.name, tl.locale
FROM
    theList AS tl
WHERE
    tl.id NOT IN (SELECT est_id
                  FROM user_hidden_list
                  WHERE userid = X AND hidden = 1);

I'm sure there will be a better (elegant) way to do this, but i'm just leaving work and my mind is not working nice now.

Shidersz
  • 16,846
  • 2
  • 23
  • 48
0

I am trying to display all results except where in Table 2 both the userid column equals 1 and the hidden column equals 1.

This does not suggest an outer join. You seem to want:

SELECT tl.id, tl.name, tl.locale, uh.hidden 
FROM theList tl JOIN
      user_hidden_list uh
      ON uh.est_id = tl.id 
WHERE NOT (uh.userid = 1 AND uh.hidden = 1);

I am guessing that userid and hidden are numbers of some sort, so I removed the single quotes. If they are really strings, then use the single quotes.

This also assumes that these values cannot be NULL. If that is a possibility, then the logic can be adjusted (using the null-safe comparator <=>).

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Thanks for the help! Unfortunately I still can't get it to work! I have added some more details to the question! – tbowden Oct 16 '18 at 21:09
0

Try out this, with even parantheses:

SELECT tl.id, tl.name, tl.locale, uh.hidden 
FROM theList tl JOIN
      user_hidden_list uh
      ON uh.est_id = tl.id 
WHERE (uh.userid <> 1 AND uh.hidden <> 1);
Gratien Asimbahwe
  • 1,606
  • 4
  • 19
  • 30