简体   繁体   English

一个查询中的MySQL SELECT,LEFT JOIN和COUNT()返回错误

[英]MySQL SELECT, LEFT JOIN and COUNT() in one query returns error

I got stuck with fairly plain query! 我陷入了相当普通的查询!

Briefly, I have created a search (filter) panel and now I'm adding pagination to it. 简要地说,我已经创建了一个搜索(过滤器)面板,现在我在其中添加了分页功能。

I'm having a problem in returning the number of rows that is in current query, mostly dependent on dynamically changing $detailed_search_query variable. 我在返回当前查询中的行数时遇到问题,主要取决于动态更改$detailed_search_query变量。

I need to make the following work, with adding COUNT() to it properly, so the new row total would contain the overall number of unique_id 's. 我需要通过适当添加COUNT()来进行以下工作,因此新的总计行将包含unique_id

Current SQL: 当前的SQL:

$sql = $db->prepare( "
              SELECT
                individuals.individual_id,
                individuals.unique_id,
                individuals.fullname,
                individuals.day_of_birth,
                TIMESTAMPDIFF(YEAR,individuals.day_of_birth,CURDATE()) AS age,
                individuals.gender,
                individuals.record_timestamp,
                individuals.active,
                individuals.deleted,
                individuals_dynamics.weight,
                individuals_dynamics.degree,
                individuals_dynamics.trainer_name
              FROM
                individuals as individuals
              LEFT JOIN
                individuals_dynamics AS individuals_dynamics ON individuals.unique_id = individuals_dynamics.individual_id
              WHERE
                $detailed_search_query $display recognized = 'yes' 
              GROUP BY
                individuals.record_timestamp
              ORDER BY $by $how
              LIMIT " . $limit);

If I add COUNT() to it, I have PDO error saying Fatal error: Call to a member function execute() on a non-object . 如果将COUNT()添加到它,则会出现PDO错误,提示Fatal error: Call to a member function execute() on a non-object

This is how my new query ( just beginning, rest is the same ) looks like, that returns error above: 这是我的新查询(刚刚开始,其余部分相同)的样子,返回上面的错误:

$sql = $db->prepare( "
                  SELECT
                    COUNT(individuals.unique_id),
                    individuals.individual_id,
                    individuals.unique_id,
                    individuals.fullname,
                    individuals.day_of_birth,

What am I missing here? 我在这里想念什么?

EDIT 1: 编辑1:

The example of how I use COUNT() results in plain pre-query that works: 我如何使用COUNT()的示例导致普通的预查询有效:

 $sql              = $db->prepare("SELECT count(unique_id) FROM individuals");
 $sql->execute();
 $total            = $sql->fetchColumn();
 $pagination       = new Pagination($paginate_number);
 $limit            = $pagination->getLimit($total);

EDIT 2: 编辑2:

Yes, right, when I add an alias same error returns, example: 是的,正确,当我添加别名时,会返回相同的错误,例如:

$sql = $db->prepare( "
                  SELECT
                    COUNT(individuals.unique_id) as total,
                    individuals.individual_id,

EDIT 3: 编辑3:

It's my bad about the last EDIT, if you add alias, like as total, then query works BUT it only COUNTS current row and returns 1 but I need total row count, example: 这对我的最后一次编辑不利,如果您添加别名(如总数),则查询有效,但它仅对当前行进行计数并返回1,但我需要总行数,例如:

Array
(
    [0] => Array
        (
            [total] => 1
            [0] => 1
            [individual_id] => 51
            [1] => 51
            [unique_id] => f598edae
            [2] => f598edae

EDIT 4: 编辑4:

When the PHP variables are replaced then I have something like this in WHERE clause: 当替换PHP变量时,我在WHERE子句中有以下内容:

                  WHERE
                    individuals.fullname LIKE '%adam%' AND individuals_dynamics.degree BETWEEN '1' AND '3' AND EXTRACT(YEAR FROM (FROM_DAYS(DATEDIFF(NOW(),individuals.day_of_birth))))+0 BETWEEN '7' AND '10' AND individuals_dynamics.weight BETWEEN  '20' AND '40' AND individuals_dynamics.degree BETWEEN '7' AND '10' AND deleted != 'yes' AND active != 'no' AND recognized = 'yes' 
                  GROUP BY
                    individuals.record_timestamp

EDIT 5: 编辑5:

The desired result would be to have in a final array the key total, that would represent the total amount of results that were extracted in the current query based on dynamic PHP variables, as $detailed_search_query and $display : 期望的结果是在最终数组中包含键合计,该合计代表基于动态PHP变量在当前查询中提取的结果总数,如$detailed_search_query$display

Now I have always 1 in the total. 现在我总有1个。 When it should be 75: 应该是75:

Array
(
    [0] => Array
        (
            [total] => 1
            [0] => 1
            [individual_id] => 71
            [1] => 71
            [unique_id] => f598e2ae
            [2] => f598e2ae
            [fullname] => Name2 Name2 Name2
        )

    [1] => Array
        (
            [total] => 1
            [0] => 1
            [individual_id] => 65
            [1] => 65
            [unique_id] => b76497ca
            [2] => b76497ca

        )

The error that you get means that PDO can't prepare the query, and the reason is that there is an error in your SQL query and the database server can't execute it ... So to let understand better the question you should post the error that you get trying to executing the query on the mysql client directly . 您收到的错误意味着PDO无法准备查询,原因是SQL查询中存在错误,数据库服务器无法执行它。因此,为了更好地理解该问题,您应该发布您尝试直接在mysql客户端上执行查询的错误。

To achieve the result set you need, you can insert a subquery that count the records that have the same individual_id as the outer query. 为了达到你所需要的结果集,你可以插入一个子查询计数具有相同的记录individual_id作为外部查询。

Following the first part of the query : 在查询的第一部分之后:

SELECT 
(SELECT COUNT(unique_id) FROM individuals i2 WHERE i2.individual_id = individuals. individual_id) AS total,
individuals.individual_id,
individuals.unique_id,
individuals.fullname,
individuals.day_of_birth,

Bear in mind that to reference a column of the outer query correctly from the subquery you should use two different alias name, even if you are selecting from the same table in both query (outer and sub). 请记住,要从子查询中正确引用外部查询的列,即使您从两个查询(外部和子)的同一表中进行选择,也应使用两个不同的别名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM