How can I pick up items not viewed?

Asked

Viewed 42 times

0

Use the following select to pick up 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 intend to pick by a select all unseen news, IE, if I didn’t view it, don’t have my ip related to a news id.

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

How can I make this such a select to pick only news not viewed? I hope that has been made clear.

  • 1

    Already tried using the operator <> or != ? $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");

  • Just use the above query select all the nodes that do not contain the user ip.

  • I used yes, however, it does not return anything, because it only has records of my ip in the table "views".

  • And as I said, the "views" table only records when someone accesses the news. I think a logic for this would be to check if my ip exists related to news, but in the query I do not know how to do this.

1 answer

0

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

$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");

Browser other questions tagged

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