How do I pick up unseen items?

0

I use the following select to grab the items already seen:

$sql = $conn->query("SELECT * FROM views b, news a WHERE a.status = 'true' AND a.id = b.id_news AND b.ip = '$ip' ORDER BY a.id DESC LIMIT 6");

Where: $ip = $_SERVER['REMOTE_ADDR']; .

Table Structures:

  • views

    id | id_news | ip

  • news

    id | title | text | status

I want to get a select all the unseen news, ie if I did not see it, it does not have my ip related to the id of a news.

I always feed the table views when a user enters the news, there is inserted the news id and the user's ip.

How can I make this select to pick only unseen news? I hope it has become clear.

    
asked by anonymous 21.12.2017 / 00:58

1 answer

0

Studying a little more about sql I got to the expected result. I hope to help other people if they have a related question. Here is the final result of the query:

$sql = self::query("SELECT * FROM news a WHERE NOT EXISTS (SELECT * FROM views b WHERE b.id_news = a.id AND b.ip = '$ip') ORDER BY a.id DESC LIMIT 6");
    
21.12.2017 / 03:45