简体   繁体   中英

How to find mutual friends and their count from a mysql table using cakephp find

How can i fetch a record having same link and show how many times in repeats in database along with its count. Sample data and table structure is mentioned below. I am using cakephp framework and my query looks like as below:

Here these records have same link and i need to show this record only once along with it count here it will 2.

$socialList =   $this->SocialImportGeneral->find('all',array(
                                'joins'=>$where,
                                'conditions'=>$conditions,
                                'fields'=>array(
                                    'SocialImportGeneral.social_import_general_id',
                                    'SocialImportGeneral.display_name',
                                    'SocialImportGeneral.designation',
                                    'SocialImportGeneral.company',
                                    'SocialImportGeneral.industry',
                                    'SocialImportGeneral.location',
                                    'SocialImportGeneral.image',
                                    'SocialImportGeneral.link',
                                    'SocialImportGeneral.processed',
                                    'SocialImportGeneral.remarks',
                                    'SocialImportGeneral.social_import_revision_id',
                                    'SocialImportRevision.fourthambit_id'
                                ),
                                'limit'=>15,
                                'offset'=>$offset,
'group'=>array('SocialImportGeneral.display_name')
                            ));

This is my table structure:

CREATE TABLE IF NOT EXISTS `social_import_general` (
  `social_import_general_id` int(11) NOT NULL AUTO_INCREMENT,
  `id` double DEFAULT NULL,
  `display_name` varchar(450) DEFAULT NULL,
  `designation` varchar(750) DEFAULT NULL,
  `company` varchar(600) DEFAULT NULL,
  `industry` varchar(600) DEFAULT NULL,
  `location` varchar(600) DEFAULT NULL,
  `search` varchar(750) DEFAULT NULL,
  `image` varchar(600) DEFAULT NULL,
  `link` varchar(750) DEFAULT NULL,
  `social_import_revision_id` int(11) DEFAULT NULL,
  `processed` tinyint(2) NOT NULL DEFAULT '0' COMMENT '0 - default 1 - no action take 2 - onhold',
  `remarks` varchar(250) DEFAULT NULL,
  `import_type` tinyint(1) NOT NULL COMMENT '1 =  fileimport , 2 = friendslist ',
  `ref_user_id` int(11) DEFAULT NULL COMMENT 'The user from which this record is fetched from social_login_friends table',
  `social_directory_id` int(11) DEFAULT NULL,
  `created_date` datetime NOT NULL,
  `modified_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `status` tinyint(1) NOT NULL DEFAULT '1',
  `delete_status` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`social_import_general_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Here i have records like:

3253
NULL
Sunny
NULL
NULL
NULL
NULL
NULL
https://graph.facebook.com/759390230759125/picture...
https://www.facebook.com/profile.php?id=7593902307...
73
0
NULL
2
10443
2
2015-04-09 11:28:53
2015-04-09 11:32:43
1
0

========================

3253
NULL
Sunny
NULL
NULL
NULL
NULL
NULL
https://graph.facebook.com/759390230759125/picture...
https://www.facebook.com/profile.php?id=7593902307...
73
0
NULL
2
10444
2
2015-04-09 11:28:53
2015-04-09 11:32:43
1
0

I tried group by but did not worked. I need to get the count for repeated records having same link in this table. Any help is appreciated.

$socialList =   $this->SocialImportGeneral->find('all',array(
                                'joins'=>$where,
                                'conditions'=>$conditions,
                                'fields'=>array(
                                    'count(*) as TotalCount',
                                    'SocialImportGeneral.social_import_general_id',
                                    'SocialImportGeneral.display_name',
                                    'SocialImportGeneral.designation',
                                    'SocialImportGeneral.company',
                                    'SocialImportGeneral.industry',
                                    'SocialImportGeneral.location',
                                    'SocialImportGeneral.image',
                                    'SocialImportGeneral.link',
                                    'SocialImportGeneral.social_directory_id',
                                    'SocialImportGeneral.email_id',
                                    'SocialImportGeneral.processed',
                                    'SocialImportGeneral.remarks',
                                    'SocialImportGeneral.social_import_revision_id',
                                    'SocialImportRevision.fourthambit_id'
                                ),
                                'limit'=>15,
                                'offset'=>$offset,
                                'group'=>array('SocialImportGeneral.link,SocialImportGeneral.email_id HAVING COUNT(*) >= 1'),
                                'order'=>'TotalCount DESC',
                            ));

Group By with order by did the trick for me thank you all....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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