Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cakephp修改联接查询以添加计数,而不是获取表的所有详细信息_Php_Mysql_Cakephp - Fatal编程技术网

Cakephp修改联接查询以添加计数,而不是获取表的所有详细信息

Cakephp修改联接查询以添加计数,而不是获取表的所有详细信息,php,mysql,cakephp,Php,Mysql,Cakephp,我试图得到最近10个视频连同他们的总看法和喜欢,如果我喜欢或不喜欢的视频 这是我的密码 $videos = $this->Video->find('all', array( 'joins' => array( array( 'table' => 'video_views',

我试图得到最近10个视频连同他们的总看法和喜欢,如果我喜欢或不喜欢的视频

这是我的密码

 $videos = $this->Video->find('all', array(
                        'joins' => array(
                                array(
                                    'table' => 'video_views',
                                    'alias' => 'views',
                                    'type' =>  'INNER',
                                    'conditions' => array(
                                        'views.userId = Video.id',                                        
                                    )
                                ),
                                array(
                                    'table' => 'video_likes',
                                    'alias' => 'likes',
                                    'type' =>  'INNER',
                                    'conditions' => array(
                                        'likes.userId = Video.id',                                        
                                    )
                                )
                        ),
                        'fields' => array('Video.*'),            
                        'order' => array('Video.creationDate DESC'),
                        'limit' => 10,
                        'offset' => $offset * 10
                ));
目前,它正在获得10个最新视频、他们的喜好数据和浏览数据

但是,我希望修改查询以获得count(*),而不是合并所有视图/数据

类似视频['ViewScont']=xxx的东西

第二,我想从喜欢表中知道我是否喜欢那个特定的视频

如何修改此查询

感谢@newbeeDev

$videos = $this->Video->find('all', array(
                'fields' => array(
                        'Video.*',
                        'Count(View.id) as views',
                        'Count(Like.id) as likes',
                        '(CASE when Liked.id is not null then 1 else 0 end) as liked'
                ),
                'joins' => array(
                        array(
                                'type' => 'LEFT',
                                'table' => 'video_views',
                                'alias' => 'View',
                                'conditions' => array('Video.id = View.videoId')
                        ),                          
                        array(
                                    'type' => 'LEFT',
                                    'table' => 'video_likes',
                                    'alias' => 'Like',
                                    'conditions' => array('Video.id = Like.videoId')
                            ),
                        array(
                                'type' => 'LEFT',
                                'table' => 'video_likes',
                                'alias' => 'Liked',
                                'conditions' => array('Video.id = Liked.videoId AND Liked.userId = ' . $userId)
                        ),
                ),
                'group' => array(
                        'Video.id'
                ),
                'order' => array('Video.creationDate DESC'),
                'limit' => 10,
                'offset' => $offset * 10
        ));

只需将连接替换为

    'joins' => array(
            array(
                    'table' => 'video_views',
                    'alias' => 'View',
                    'conditions' => array('Video.id = View.video_id')
            ),
            array(
                    'type' => 'LEFT',
                    'table' => 'video_likes',
                    'alias' => 'Like',
                    'conditions' => array('Video.id = Like.video_id and Like.user_id = 1')
            ),
    ),
您的查询生成器如下所示

         $videos = $this->Video->find('all', array(
            'fields' => array(
                    'Video.*',
                    'Count(View.id) as views',
                    '(CASE when Like.id is not null then 1 else 0 end) as liked'
            ),
            'joins' => array(
                    array(
                            'table' => 'video_views',
                            'alias' => 'View',
                            'conditions' => array('Video.id = View.video_id')
                    ),
                    array(
                            'type' => 'LEFT',
                            'table' => 'video_likes',
                            'alias' => 'Like',
                            'conditions' => array('Video.id = Like.video_id and Like.user_id = 1')
                    ),
            ),
            'group' => array(
                    'Video.id'
            ),
            'order' => array('Video.creationDate DESC'),
            'limit' => 10,
            'offset' => $offset * 10
    ));

您想要的查询结果到底是什么?把它包括在你的计划中post@NewbeeDev需要添加视频['likescont']=xxx,而不是当前正在进行的视频['likes']=array(),它工作得很好,但是如果视频没有任何喜欢的内容或视图,它就不会显示that@MuhammadUmar只需在加入中添加“type”=>“left”,它不会显示,因为join单独需要匹配项,但使用left join,即使没有匹配项,它也会显示