Jump to content

Recommended Posts

Hello everyone... I have spent about 2 weeks trying to solve this problem... Here we go...

 

I have an array of ProfileIDs that looks like this...

Array

(

    [3] => 3

    [4] => 4

    [8] => 8

)

 

I have a table that looks like this...

ProfileID | Interest

 

Every ProfileID could have multiple Interests, so multiple rows...

 

What I need to do is query that table that searches for those ProfileIDs as well as second set of Array Values, called Interests, and some how end up with only those IDs that ONLY have ALL of the second set, the Interests, of Array Values...

 

So here's an example of kind of where I'm at right now...

 

$firstProfileID_Array[] = '';
foreach ($RemainingProfiles_Array as $RemainingProfile_Member) {
    foreach ($Interest_Array as $Interest_Element) {
        $query = mysql_query("SELECT * FROM Interests WHERE ProfileID = '$RemainingProfile_Member' AND Interest = '$Interest_Element'");
        while ($row = mysql_fetch_assoc($query)) {
            $firstProfileID_Array[$row['ProfileID']] = $row['ProfileID'];
        }
    }
}

 

But what that leaves me with, as I'm sure you can imagine, is a list of all ProfileIDs that contain AT LEAST ONE of the Interests...

Link to comment
https://forums.phpfreaks.com/topic/239022-array-problem/
Share on other sites

Here's what the SQL could look like. Execute this once to get all the matching profile IDs.

SELECT ProfileID FROM table WHERE ProfileID IN (3, 4,  AND Interest IN (9, 8, 7, 6) GROUP BY ProfileID HAVING COUNT(1) = 4

(That 4 is the number of interests.)

 

This assumes the table has no duplicate rows.

Link to comment
https://forums.phpfreaks.com/topic/239022-array-problem/#findComment-1228126
Share on other sites

Here's what the SQL could look like. Execute this once to get all the matching profile IDs.

SELECT ProfileID FROM table WHERE ProfileID IN (3, 4,  AND Interest IN (9, 8, 7, 6) GROUP BY ProfileID HAVING COUNT(1) = 4

(That 4 is the number of interests.)

 

This assumes the table has no duplicate rows.

 

So how would I get the array values listed out like that in the array, in the query? (3, 4, 8) & (9, 8, 7, 6)

The count I figure I can do sizeof() function to get that...

 

Link to comment
https://forums.phpfreaks.com/topic/239022-array-problem/#findComment-1228132
Share on other sites

If you mean how do you get them into the query from PHP (I'm assuming they are integers)

$sql = "SELECT ProfileID FROM table
WHERE ProfileID IN (" . implode(",", $profile_id_array) . ")
AND Interest IN (" . implode(",", $interest_array) . ")
GROUP BY ProfileID HAVING COUNT(*) = " . count($interest_array);

 

If you mean, how do you get them as a column returned by the query ...

$sql = "SELECT ProfileID, GROUP_CONCAT(Interest SEPARATOR ',') AS Interests FROM table
WHERE ProfileID IN (" . implode(",", $profile_id_array) . ")
AND Interest IN (" . implode(",", $interest_array) . ")
GROUP BY ProfileID HAVING COUNT(*) = " . count($interest_array);

I guess that second statement is redundant since they will be the same as provided in the query.

Link to comment
https://forums.phpfreaks.com/topic/239022-array-problem/#findComment-1228144
Share on other sites

If you mean how do you get them into the query from PHP (I'm assuming they are integers)

$sql = "SELECT ProfileID FROM table
WHERE ProfileID IN (" . implode(",", $profile_id_array) . ")
AND Interest IN (" . implode(",", $interest_array) . ")
GROUP BY ProfileID HAVING COUNT(*) = " . count($interest_array);

 

If you mean, how do you get them as a column returned by the query ...

$sql = "SELECT ProfileID, GROUP_CONCAT(Interest SEPARATOR ',') AS Interests FROM table
WHERE ProfileID IN (" . implode(",", $profile_id_array) . ")
AND Interest IN (" . implode(",", $interest_array) . ")
GROUP BY ProfileID HAVING COUNT(*) = " . count($interest_array);

I guess that second statement is redundant since they will be the same as provided in the query.

 

The Interests array are not integers, will that not work then?

Link to comment
https://forums.phpfreaks.com/topic/239022-array-problem/#findComment-1228145
Share on other sites

Then we have to wrap them in quotes:

 

$sql = "SELECT ProfileID, GROUP_CONCAT(Interest SEPARATOR ',') AS Interests FROM table
WHERE ProfileID IN (" . implode(",", $profile_id_array) . ")
AND Interest IN ('" . implode("', '", $interest_array) . "')
GROUP BY ProfileID HAVING COUNT(*) = " . count($interest_array);

Link to comment
https://forums.phpfreaks.com/topic/239022-array-problem/#findComment-1228147
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.