Question

Please take a look at the following query:

SELECT tc.registered, t.id, u.x_account_username
                        FROM x_trybe_comments tc
                        LEFT JOIN x_user u
                        ON tc.profile_id = u.id
                        LEFT JOIN x_trybes t
                        ON tc.trybe_id = t.id
                        WHERE u.id IN ('%s')
                        AND tc.trybe_id IN ('%s')

$query      =   sprintf($query, $contacts, $trybes);

I'm trying to select all comments from x_trybe_comments, whose my contact

i.e. u.id IN ('%s')

AND in my trybes

i.e. tc.trybe_id IN ('%s')

The '%s' are replaced with implode()'s of arrays.

I get the arrays from two functions as follows:

    $contacts = getContactIds($db,$profile_id);
    $trybes = getTrybesJoinedOrOwned($db,$profile_id);
    $contacts = implode("', '", $contacts);
    $trybes = implode("', '", $trybes);

Ouput for trybes and contacts:

Trybes: c341f36cc31c7e22c4ec00fd484c1fc03a7b2dfd', 'baa764365ac0a4ba044651ca46b08aa8de74e288', 'a9f4fba45750075eccda69f6645d6240c8889b10', '4ce48a7c7c21f82dc632bde72305d1330e1ac28 

Contacts: 1dd36ac747735a3ee8a1d47750e1515ab7ac0d53', 'dc19818afa52859032bdde290b900e3763ba54d0', '397cde4302885e167004494340971f4550346e04', 'c73fb8efa74354e721a5a945a5c367f486a3d80b', 'c484c72086ed2fdbd859954a2a753b07c4bd1f1d

Here is an echo of the query in question:

SELECT tc.registered, t.id, u.x_account_username
                        FROM x_trybe_comments tc
                        LEFT JOIN x_user u
                        ON tc.profile_id = u.id
                        LEFT JOIN x_trybes t
                        ON tc.trybe_id = t.id
                        WHERE u.id IN ('1dd36ac747735a3ee8a1d47750e1515ab7ac0d53', 'dc19818afa52859032bdde290b900e3763ba54d0', '397cde4302885e167004494340971f4550346e04', 'c73fb8efa74354e721a5a945a5c367f486a3d80b', 'c484c72086ed2fdbd859954a2a753b07c4bd1f1d')
                        AND tc.trybe_id IN ('c341f36cc31c7e22c4ec00fd484c1fc03a7b2dfd', 'baa764365ac0a4ba044651ca46b08aa8de74e288', 'a9f4fba45750075eccda69f6645d6240c8889b10', '4ce48a7c7c21f82dc632bde72305d1330e1ac280', '408032a6043f6789083c4919474969f6d47abb1c', 'f8fbf59d242794cc6c00932a8ef643587338d189', '76fb8332f5808111f6e83e5a8f9068ff911850e5', 'd62243e0026e8d4c48143549b421e492ba1a840f', '580861afde002c536466db9ae953e43c47b3eeab', 'faebdc82b8bcf8fbb5db56c3c3b805f3e1fda5a2', '297cebfa7522e253d65d1fd6f5f904ca5b626905')

Can you see what I'm doing wrong?

Thanks

Was it helpful?

Solution

I'm an idiot, I was returning the $result object instead of my $array of data

Thanks for commenting, problem solved!

OTHER TIPS

In terms of the query, yes. First problem: you are using left join, but the where conditions are undoing the left join, turning it into a mere inner join. The problem is that column values are NULL in unmatched rows, and the where clause does not take this into account.

The right solution is to move the conditions to the on clauses:

SELECT tc.registered, t.id, u.x_account_username
FROM x_trybe_comments tc LEFT JOIN
     x_user u
     ON tc.profile_id = u.id AND u.id IN ('%s') LEFT JOIN
     x_trybes t
     ON tc.trybe_id = t.id AND tc.trybe_id IN ('%s');

Or, if you really mean INNER JOIN, which seems likely:

SELECT tc.registered, t.id, u.x_account_username
FROM x_trybe_comments tc INNER JOIN
     x_user u
     ON tc.profile_id = u.id INNER JOIN
     x_trybes t
     ON tc.trybe_id = t.id 
WHERE u.id IN ('%s') AND tc.trybe_id IN ('%s');

The next problem is that the in condition will only work for one value. in ('2,3') is different from in (2,3). One way to solve this is by putting the values into the query. Another is to use find_in_set():

SELECT tc.registered, t.id, u.x_account_username
FROM x_trybe_comments tc INNER JOIN
     x_user u
     ON tc.profile_id = u.id INNER JOIN
     x_trybes t
     ON tc.trybe_id = t.id 
WHERE find_in_set(u.id, '%s') AND find_in_set(tc.trybe_id, '%s');

I would consider find_in_set() to be the quick/dirty solution. Actually fixing the query is better because it can then use indexes and does not use a MySQL-specific function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top