2

我有一个获取搜索结果的查询,效果很好。

查询成功示例:

 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

从现在开始,我每个人都有不止一个记录,individuals_achievements这是我想获得 MAX 值(最新 id)的地方。

我尝试了许多不同的查询,但总是收到错误调用非对象上的成员函数 rowCount()。

我理解这个错误的含义,但我不知道我在哪里犯了这个错误,以及一般是什么错误。

我不成功的尝试示例:

  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

我在这里想念什么?请问有什么帮助吗?

4

5 回答 5

2

这是你的from条款:

  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;

我至少可以发现三个问题。第一个是individuals_achievements AS individuals_achievements;第二个是individuals_achievements.individual_id不在子查询中的引用。第三个是group by latest_record

  FROM individuals LEFT JOIN
       individuals_dynamics
       ON individuals.unique_id = individuals_dynamics.individual_id LEFT JOIN
       individuals_achievements
       ON individuals.unique_id = individuals_achievements.individual_id JOIN
       (SELECT ia.individual_id, MAX(ia.id) AS latest_record
        FROM individuals_achievements ia
        GROUP BY ia.individual_id
       ) iamax
       ON individuals.unique_id = iamax.individual_id and
          individuals_achievements.id = iamax.latest_record

这添加了一个额外的子查询,带有最新记录的 id。

顺便说一句,让表别名与表名同名是多余的。这只会使查询变得混乱。此外,为别名使用表缩写是一个好主意,例如iafor individuals_achievements。因为这个答案只关注from条款,所以我没有做出改变。

于 2013-09-19T19:16:26.587 回答
0

PDO::queryFALSE如果在处理您的查询时出现错误,则返回 a ,并且将rowCounttoFALSE作为成员函数应用将导致您看到的错误。

检查您的查询是否有语法错误,并解析任何PDO返回给您的错误消息。

于 2013-09-19T19:08:42.667 回答
0
INNER JOIN
  (
       SELECT
            degree, MAX(id) AS latest_record,individual_id
       FROM
            individuals_achievements
       GROUP BY
            latest_record
  ) individuals_achievements AS individuals_achievements_1 ON individuals.unique_id = individuals_achievements_1.individual_id

它应该是

INNER JOIN (SELECT blah) AS something ON individuals.unique_id=something.individual_id

于 2013-09-19T19:14:09.687 回答
0

您必须在您的子句中进行选择individual_idINNER JOIN因为您在ON子句中使用的那部分,并且您只选择degree, MAX(id) AS latest_record并尝试individual_id通过内部连接在新生成的表中加入未定义的部分

individuals_achievements AS individuals_achievements 
ON individuals.unique_id = individuals_achievements.individual_id

试试这个

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,individual_id
       FROM
            individuals_achievements
       GROUP BY
            latest_record
  ) individuals_achievements AS individuals_achievements_1 ON individuals.unique_id = individuals_achievements_1.individual_id
  WHERE
    $uuid_access_status $display_type $detailed_search_query
  ORDER BY 
    $search_sort $search_order

确保您individual_id在表格中确实有一列individuals_achievements

于 2013-09-19T19:14:42.577 回答
0

首先,您并没有真正使用ASSQL 中的关键字来使您的查询更具可读性,这将有所帮助。我想知道WHERE子句的 (php?) 变量中有什么。

此外,您的子查询没有返回一个字段individual_id,但您尝试加入它。我想你可能想要这个:

SELECT
  ind.individual_id,
  ind.unique_id,
  TIMESTAMPDIFF(YEAR,ind.day_of_birth,CURDATE()) AS age,
  ind_dyn.id,
  ind_ach.degree
FROM
  individuals as ind
LEFT JOIN
  individuals_dynamics AS ind_dyn ON ind.unique_id = ind_dyn.individual_id
INNER JOIN
  individuals_achievements AS ind_ach 
     ON ind_ach.individual_id = ind.unique_id
    AND ind_ach.id =
    (
      SELECT MAX(id) FROM individuals_achievements
      WHERE individuals_achievements.individual_id = ind.unique_id
    )
WHERE
  $uuid_access_status $display_type $detailed_search_query
ORDER BY 
  $search_sort $search_order
于 2013-09-19T19:20:29.313 回答