在 CakePHP 查找函数中使用 DISTINCT
我正在编写一个 CakePHP 1.2 应用程序。我有一个人员列表,我希望用户能够在不同字段上进行过滤。对于每个可过滤字段,我都有一个下拉列表。选择过滤器组合,单击过滤器,页面仅显示匹配的记录。
在 people_controller 中,我有这样的代码:(
$first_names = $this->Person->find('list', array(
'fields'=>'first_name',
'order'=>'Person.first_name ASC',
'conditions'=> array('Person.status'=>'1')
));
$this->set('first_names', $first_names);
状态 = 1,因为我使用软删除。)
这会创建所有名字的有序列表。但那里有重复的东西。
在 Cookbook 中深入研究,我找到了一个使用 DISTINCT 关键字的示例,并修改了我的代码以使用它。
$first_names = $this->Person->find('list', array(
'fields'=>'DISTINCT first_name',
'order'=>'Person.first_name ASC',
'conditions'=> array('Person.status'=>'1')
));
这给了我一个像这样的 SQL 错误:
Query: SELECT `Person`.`id`, DISTINCT `Person`.` first_name` FROM `people` AS `Person` WHERE `Person`.`status` = 1 ORDER BY `Person`.`first_name` ASC
问题很明显。该框架正在将 Person.id 添加到查询中。我怀疑这来自于使用“列表”。
单击过滤器按钮时,我将使用选定的过滤器创建 SQL 语句。我不需要 is 字段,但无法摆脱它。
谢谢你, 弗兰克·卢克
I am writing a CakePHP 1.2 app. I have a list of people that I want the user to be able to filter on different fields. For each filterable field, I have a drop down list. Choose the filter combination, click filter, and the page shows only the records that match.
In people_controller, I have this bit of code:
$first_names = $this->Person->find('list', array(
'fields'=>'first_name',
'order'=>'Person.first_name ASC',
'conditions'=> array('Person.status'=>'1')
));
$this->set('first_names', $first_names);
(Status = 1 because I am using a soft delete.)
That creates an ordered list of all first_names. But duplicates are in there.
Digging around in the Cookbook, I found an example using the DISTINCT keyword and modified my code to use it.
$first_names = $this->Person->find('list', array(
'fields'=>'DISTINCT first_name',
'order'=>'Person.first_name ASC',
'conditions'=> array('Person.status'=>'1')
));
This gives me an SQL error like this:
Query: SELECT `Person`.`id`, DISTINCT `Person`.` first_name` FROM `people` AS `Person` WHERE `Person`.`status` = 1 ORDER BY `Person`.`first_name` ASC
The problem is obvious. The framework is adding Person.id to the query. I suspect this comes from using 'list'.
I will use the selected filter to create an SQL statement when the filter button is clicked. I don't need the is field, but can't get rid of it.
Thank you,
Frank Luke
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
尝试使用“group by”,效果完美:
Try to use 'group by', it works perfectry:
你是对的,似乎你不能将
DISTINCT
与list
一起使用。由于您不需要id
而只需要名称,因此您可以像上面一样使用find all
,然后$first_names = Set::extract($first_names, ' /Person/first_name');
。这将为您提供一个具有不同名字的数组。You're right, it seems that you cannot use
DISTINCT
withlist
. Since you don't needid
but only the names, you can usefind all
like above and then$first_names = Set::extract($first_names, '/Person/first_name');
. That will give you a array with distinct first names.以下是我在 CakePHP 3.x 中的做法:
Here's how I did it in CakePHP 3.x:
你可以试试这个。
这里以 Person id 作为键,因此不会有重复的条目。
You can try this.
Here this takes Person id as key, so there is no chance for duplicate entries.
使用 SQL 分组也会生成一个不同的列表。不确定是否有负面后果,但它似乎对我来说效果很好。
Using SQL grouping will also produce a distinct list. Not sure of the negative consequences if any, but it seems to work fine for me.
我知道这是 CakePHP 1.2 的问题,但我也在 CakePHP 版本 3 中搜索这个问题。在这个版本中,有一种方法可以将查询形成一个不同的查询:
这将生成一个像这样的 sql 查询:
但是
distinct
方法比仅在查询中插入DISTINCT
更强大。如果你只想区分一个字段上的结果,那么只需扔掉一行具有重复名称的行,你还可以使用以字段数组作为参数的 unique 方法:
这将通用一个像这样的 sql 查询(当然这是一个组查询):
希望我能为 CakePHP 3 提供一些帮助。:)
I know it is a question for CakePHP 1.2, but I was searching for that too with CakePHP version 3. And in this version there is a method to form the Query into a distinct one:
This will generate a sql query like this:
But the
distinct
method is a bit mightier than just insertingDISTINCT
in the query.If you want to just distinct the result on one field, so just throwing away a row with a duplicated name, you can also use the distinct method with an array of the fields as parameter:
This will general a sql query like this (for sure it is a group query):
Hope I will help some for CakePHP 3. :)
是的,问题是您正在使用专为 id / value 输出设计的列表。您可能必须执行 find('all') 然后自己构建列表。
Yes the problem is that you are using a listing which designed for a id / value output. You probably will have to do a find('all') and then build the list yourself.
是的,我也尝试使用“列表”获取独特的结果,但它不起作用。然后我使用“all”解决了这个问题。
Yes I also tried to fetch unique results with 'list' but its not working. Then I fixed the problem by using 'all'.
在现在的版本 2 的书籍的示例中声明如下:
对于上面的代码示例,生成的变量将如下所示:
您必须以稍微不同的方式规划查询,并规划数据库以容纳列表查找。
In the example now on the book for version 2 it states the following:
With the above code example, the resultant vars would look something like this:
You have to plan your query in a slightly different way and plan your database to accommodate a list find.
在某些情况下,您希望使用某个键进行分组,但您希望结果中包含唯一的元素。
例如,您有一个包含两种类型事件的日历应用程序。一个事件发生在第一个月的第一天,另一个事件发生在第一个月的第二天。您想要显示或统计所有事件,按天和类型分组。
如果只使用
DISTINCT
,那就相当困难了。最简单的解决方案是分组两次:In some cases, you wish to group by using some key, but you want unique element within the results.
For example, you have a calendar application with two types of events. One event on day 1rst and the other one on the 2nd day of month 1. And you want to show or rather count all the events, grouped by day and by type.
If you use only
DISTINCT
, it is quite difficult. The simplest solution is to group twice:只需按您想要区分的字段进行分组..或使用
Set::extract()
然后使用array_unique()
Just group by the fields that you want to get distinct.. or use
Set::extract()
and thenarray_unique()
在 2.7-RC 版本中,这是有效的
In 2.7-RC version, this is working