I have a query for getting search results, which works fine.
Example of successful query:
SELECT
individuals.individual_id,
individuals.unique_id,
TIMESTAMPDIFF(YEAR,individuals.day_of_birth,CURDATE()) AS age,
individuals_dynamics.id,
individuals_achievements.degree
FROM
individuals as individuals
LEFT JOIN
individuals_dynamics AS individuals_dynamics ON individuals.unique_id = individuals_dynamics.individual_id
LEFT JOIN
individuals_achievements AS individuals_achievements ON individuals.unique_id = individuals_achievements.individual_id
WHERE
$uuid_access_status $display_type $detailed_search_query
ORDER BY
$search_sort $search_order
From now on, I have more than one record in individuals_achievements
per each individual and this is the where I would like to get the MAX value (latest id).
I tried the many different queries but always was getting an error Call to a member function rowCount() on a non-object.
I understand what that error means but I can't figure out where I'm making that mistake and what is wrong in general.
Example of my unsuccessful attempt:
SELECT
individuals.individual_id,
individuals.unique_id,
TIMESTAMPDIFF(YEAR,individuals.day_of_birth,CURDATE()) AS age,
individuals_dynamics.id,
individuals_achievements.degree
FROM
individuals as individuals
LEFT JOIN
individuals_dynamics AS individuals_dynamics ON individuals.unique_id = individuals_dynamics.individual_id
INNER JOIN
(
SELECT
degree, MAX(id) AS latest_record
FROM
individuals_achievements
GROUP BY
latest_record
) individuals_achievements AS individuals_achievements ON individuals.unique_id = individuals_achievements.individual_id
WHERE
$uuid_access_status $display_type $detailed_search_query
ORDER BY
$search_sort $search_order
What am I missing here? Any help please?
You have to select
individual_id
in yourINNER JOIN
because that part you are using in yourON
clause and you have selecteddegree, MAX(id) AS latest_record
only and trying to join withindividual_id
which is undefined in the newly generated table by inner jointry this one
Make sure you do have a column
individual_id
in tableindividuals_achievements
First off you're not really using the
AS
keyword in SQL to make your query more readable, which would help. And I'm wondering what is in the (php?) variables of theWHERE
clause.Also, your subquery isn't returning a field
individual_id
yet you try to JOIN on it. I think you might want this:This is your
from
clause:I can spot at least three problems. The first is
individuals_achievements AS individuals_achievements
; the second is the reference toindividuals_achievements.individual_id
which isn't in the subquery. The third is thegroup by latest_record
.This adds an additional subquery, with the id of the latest record.
By the way, it is redundant to have a table alias be the same name as the table name. That just clutters up the query. Also, it is a good idea to use table abbreviations for the aliases, such as
ia
forindividuals_achievements
. Because this answer focuses only on thefrom
clause, I have not made that change.It should be
INNER JOIN (SELECT blah) AS something ON individuals.unique_id=something.individual_id
PDO::query
returns aFALSE
if there's an error while processing your query, and applyingrowCount
toFALSE
as a member function will result in the error you're seeing.Check your query for syntax errors and parse any error messages
PDO
would return you.