我是新蛋糕php。我有两张桌子
结构是
ce_stat
id keyword click
1 keyword1 24
2 keyword2 2
3 keyword3 6
ce_landing
id page_keyword
1 keyword1,check,keyword3
2 keyword2,usa,alphanumeric
我想获取ce_landing.page_keyword
中存在的所有记录ce_stat.keyword
。我在我的蛋糕php模型中使用它
public $hasMany = array(
'Stat' => array(
'className' => 'Stat',
'foreignKey' => 'keywor',
'dependent' => false,
'conditions' => array('Stat.keyword LIKE' => '%Landing.page_keywords%'),
'group' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
但这是生成的SQL查询,如
SQL查询:SELECT
Stat
。id
,Stat
。customer_id
,Stat
。account
,Stat
。campaign_id
,Stat
。campaign
,Stat
。keyword
,Stat
。ad_grp_id
,Stat
。ad_grp
,Stat
。impressions
,Stat
。clicks
,Stat
。cost
,Stat
。qualityScore
,Stat
。keywordState
,Stat
。date_from
,Stat
。date_to
,Stat
。user_id
,Stat
。created
,Stat
。modified
,Stat
。keywor
FROMEB_adwords
。ce_stats
ASStat
WHEREStat
。keyword
LIKE '%Landing.page_keywords%'和Stat
。keyword
= ( '559f479a-82ac-4e3d-8c24-19b5c0a8011f')
因为AND Stat
。keyword
=('559f479a-82ac-4e3d-8c24-19b5c0a8011f')条件,它返回空数据。
更新
根据目前的关键字,我希望获得ce_landing
的所有记录的总点击次数。即ce_landing
中的记录1。我会得到结果
id page_keyword clicks
1 keyword1,check,keyword3 30
2 keyword2,usa,alphanumeric 2
答案 0 :(得分:1)
您需要一个等同于:
的SQL语句SELECT ce_landing.id, ce_landing.page_keyword, SUM(ce_stat.click) AS total_clicks
FROM ce_landing
LEFT JOIN ce_stat
ON FIND_IN_SET(keyword,page_keyword)>0
GROUP BY ce_landing.id, ce_landing.page_keyword;
不容易转换为Cakephp的find
方法。只需使用query method即可实现。
$this->Landing->query("SELECT Landing.id, Landing.page_keyword, SUM(ce_stat.click) AS total_clicks
FROM ce_landing AS Landing
LEFT JOIN ce_stat
ON FIND_IN_SET(keyword,page_keyword)>0
GROUP BY Landing.id, Landing.page_keyword;");