Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select where not equal to multiple values of keys of associative array

I had made three tables.

Table1=users.And the column names are userid (auto_increment) and username.
Table2=hobbies. column names are hobbyid (auto_increment) and hobbyname.
Table3=users_hobbies. column names are FK_userid and FK_hobbyid.

Now hobbyname has cricket,football,volleyball.
During sign-up user selects only cricket and football and I store it in $_SESSION['hobby_id'] associative array.

But in profile he/she wants to add some more hobbies.

So I want to display list of hobbies which are not equal to $_SESSION['hobby_id'].

This , ofcourse didn't work:

foreach($_SESSION['hobby_id'] as $k=>$v)
{

  $query="select hobbyid,hobbyname from hobbies where hobbyid!= $v";
}  
Output was football,volleyball and cricket,volleyball....but i wanted only volleyball

So i want that

$query="select hobbyid,hobbyname from hobbies where hobbyid!=(multiple values of session hobby id)";

What code will I have to enter to get desired result(only volleyball)?

like image 681
Santosh Avatar asked Nov 03 '22 21:11

Santosh


1 Answers

remove foreachloop and use NOT IN comparision in MySQL:

$query="
    SELECT hobbyid,hobbyname
    FROM hobbies
    WHERE hobbyid NOT IN (" . implode(',', $_SESSION['hobby_id']) . ")
";
like image 79
rabudde Avatar answered Nov 09 '22 09:11

rabudde