How do you write "Select (all) From (table) Where is posting = $ posting other than this post? (Mysql)

I want to write a Mysql statement that selects everything from a table (wiring) where the title is similar to $ title, except for the title $ title. Basically I want to display all related publications of a specific post. I want the query to select all the messages in the table whose names are indicated in the header or detail. But I donโ€™t want the post to appear in related posts.

//pseudocode $query="Select * From posting Where title,detail, like %$title% except $title"; 

how can i write the excluding part?

+4
source share
2 answers

This is the code you need, although it would be better if you have the current message id and just have something like WHERE id != " . (int)$current_post_id . "

 $title = mysql_real_escape_string($title); $sql = " SELECT * FROM posting WHERE title LIKE '%" . $title . "%' AND detail LIKE '%" . %title . "%' AND title != " . $title . " "; 

Here is the ID version, better :)

 $title = mysql_real_escape_string($title); $sql = " SELECT * FROM posting WHERE title LIKE '%" . $title . "%' AND detail LIKE '%" . %title . "%' AND id != " . (int)$post_id . " "; 
+2
source

EDIT: If you want LIKE '% $ title%' AND! = '$ Title', then:

 SELECT * FROM posting WHERE title LIKE '%$title%' AND detail LIKE '%$title%' AND title != '$title'; 

yet

 SELECT * FROM posting WHERE title LIKE '%$title%' AND detail LIKE '%$title%' AND title NOT LIKE '%$title%'; 

Are you sure right? LIKE '%$title%' AND NOT LIKE '%$title%' is a contradiction.

0
source

Source: https://habr.com/ru/post/1306538/


All Articles