Search code examples
phpsqlsql-like

How to SELECT LIKE from SQL without duplicate viewing id


I am using this code :

<?
$i_count=1;

$sSQL         = "SELECT * FROM projects WHERE status=1 AND location LIKE '%" . $a_project[0]["location"] . "%' ORDER BY delivery_year DESC, project_id DESC LIMIT 0,5";
$mysql_result = mysql_query($sSQL, $GLOBALS['conn']);
$num_rows     = mysql_num_rows($mysql_result);
if ($num_rows > 0) {
?>
<br />

It will show the project with same location with viewing project. But it also show the current project in the result. Ex : I have 02 projects in location Hanoi name A and B. When I view project A, the related projects show both A and B project.

How I can fix it ? Thanks


Solution

  • Exclude the current project in the WHERE:

    SELECT * FROM projects WHERE status=1 AND location LIKE '%" . $a_project[0]["location"] . "%' AND id <> " . $a_project[0]["id"] . " ORDER BY delivery_year DESC, project_id DESC LIMIT 0,5
    

    (Or something like it, depending on your field names and such)