我有一个名为'test'的表,如下所示:
---------------------------
id |名字|其他人 ---------------------------
1 |王| No.1 2 |张| No.1 3 |赵| No.2
---------------------------
我想对“其他人”字段进行分组,然后使用“wang”和“zhang”搜索名称。我可以使用mysql来实现它:
select others, name2
from (select *, group_concat(name separator ',') as name2
from test where 1=1 group by others) as sub
where name2 like '%wang%' and name2 like '%zhang%'
所以,我的问题是如何使用带有find或paginate的cakephp实现这一点?
感谢您的帮助!
答案 0 :(得分:1)
我不确定如何在cakehp中执行此操作,但更好的方法是编写查询:
select others, group_concat(name separator ',') as name2
from test
group by others
having sum(name = 'wang') > 0 and
sum(name = 'zhang') > 0;
这也可以防止混淆。您的版本将在kwang,zhangke
返回true。如果您确实需要此行为,请改为使用like
:
select others, group_concat(name separator ',') as name2
from test
group by others
having sum(name like '%wang%') > 0 and
sum(name like '%zhang%') > 0;