php - cakephp中特定字段的数据转义删除

标签 php mysql cakephp

我正在为 id 字段使用子查询。

$db = $this->AccountRequest->getDataSource();
        $subQuery = $db->buildStatement(
            array(
                'fields'     => array('MAX(id)'),
                'table'      => $db->fullTableName($this->AccountRequest),
                'alias'      => 'MaxRecord',
                'limit'      => null,
                'offset'     => null,
                'order'      => null,
                'group'      => array("user_id")
            ),
            $this->AccountRequest
        );

        $searching_parameters = array(
            #"AccountRequest.id IN " => "(SELECT MAX( id ) FROM `account_requests` GROUP BY user_id)"
            "AccountRequest.id IN " => "(".$subQuery.")"
        );
$this->Paginator->settings = array(
            #'fields' => array('AccountRequest.*'),
            'conditions' => $searching_parameters,
            'limit' => $limit,
            'page' => $page_number,
            #'group' => array("AccountRequest.user_id"),
            'order' => array(
                'AccountRequest.id' => 'DESC'
            )
        );

        $data = $this->Paginator->paginate('AccountRequest');

这个结构产生的查询是:

SELECT
    `AccountRequest`.`id`,
    `AccountRequest`.`user_id`,
    `AccountRequest`.`email`,
    `AccountRequest`.`emailchange`,
    `AccountRequest`.`email_previously_changed`,
    `AccountRequest`.`first_name`,
    `AccountRequest`.`first_namechange`,
    `AccountRequest`.`f_name_previously_changed`,
    `AccountRequest`.`last_name`,
    `AccountRequest`.`last_namechange`,
    `AccountRequest`.`l_name_previously_changed`,
    `AccountRequest`.`reason`,
    `AccountRequest`.`status`,
    `AccountRequest`.`created`,
    `AccountRequest`.`modified`
FROM
    `syonserv_meetauto`.`account_requests` AS `AccountRequest`
WHERE
    `AccountRequest`.`id` IN '(SELECT MAX(id) FROM `syonserv_meetauto`.`account_requests` AS `MaxRecord` WHERE 1 = 1 GROUP BY user_id)'
ORDER BY
    `AccountRequest`.`id` DESC
LIMIT 25

在子查询中,它添加了一个额外的单引号,因此会产生错误。

那么,如何从此子查询中删除这些单引号?

谢谢

最佳答案

你想通过子查询实现什么?

MAX(id) 只是意味着它将提取具有最大值的 id,也就是最近的插入。当您只需ORDER BY id DESC 时,子查询就完全多余了。

使用 MAX() 将只返回一条记录,如果这是您想要实现的,您可以通过添加 LIMIT 1

进行复制

如果子查询只是一个示例并且是来自另一个表,我将在运行主查询之前运行获取最新 ID 的查询。在单独的查询中获取最后插入的 id 非常快,我看不到太多的性能损失。我认为这会产生更清晰的代码,更易于遵循。

编辑 1: 从评论来看,您似乎只是想获得特定用户最新的 account_requests。

您根本不需要子查询。我在下面的查询将获取您选择的用户 ID 的最新帐户记录。

$this->Paginator->settings = array(
    'fields' => array('AccountRequest.*'),
    'conditions' => array(
        'AccountRequest.user_id' => $userID // you need to set the $userID
    )
    'page' => $page_number,

    'order' => array(
        'AccountRequest.id DESC' //shows most recent first
    ),
    'limit' => 1 // set however many you want the maximum to be
);

另一件你想表达的意思是从多个用户那里获取多个条目,并按照用户的顺序显示它们,然后是该用户的从新到旧的顺序。 MYSQL 允许您按多个字段排序,在这种情况下尝试:

$this->Paginator->settings = array(
    'conditions' => array(
        'AccountRequest.user_id' => $userID // you need to set the $userID
    )
    'page' => $page_number,

    'order' => array(
        'AccountRequest.user_id', //order by the users first
        'AccountRequest.id DESC' //then order there requests by recent to old
    )
);

关于php - cakephp中特定字段的数据转义删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36647057/

相关文章:

css - 如何使用 cakePHP 在 css 中添加媒体 ='screen'?

mysql - CakePHP MySQL 重复运算符操作数无效 - 生成的 SQL 工作正常,但蛋糕报告错误

php - 使用 foreach 循环时,限制查询在 codeigniter 中不起作用

php - 如何合并 php 生成的表中的单元格?

php - Javascript动态表单提交

php - 如何根据值的实例数对数组进行排序?

php - MySQL : SELECT results from four different tables in one query

python - 带有日语字符的MySQL

cakePHP - 404- page not found 错误,在不同的机器上部署时

php - url 上的 Opencart token 是 session ID 吗?