选择 *, max(date) 在 phpMyAdmin 中有效,但在我的代码中无效
好的,我的语句在 phpMyAdmin 中执行得很好,但在我的 php 页面中却不是我所期望的那样。
这是我的声明:
SELECT `egid`, `group_name` , `limit`, MAX( `date` )
FROM employee_groups
GROUP BY `egid`
ORDER BY `egid` DESC ;
这是可能的表:
CREATE TABLE `employee_groups` (
`egid` int(10) unsigned NOT NULL,
`date` date NOT NULL,
`group_name` varchar(50) NOT NULL,
`limit` smallint(5) unsigned NOT NULL,
PRIMARY KEY (`egid`,`date`)
) ENGINE=MyISAM DEFAULT CHARSET=cp1251;
我想提取最新的组列表,例如,如果组已更改,我只想拥有最后的更改。我需要它作为一个列表(所有组)。
OK, my statement executes well in phpMyAdmin, but not how I expect it in my php page.
This is my statement:
SELECT `egid`, `group_name` , `limit`, MAX( `date` )
FROM employee_groups
GROUP BY `egid`
ORDER BY `egid` DESC ;
This is may table:
CREATE TABLE `employee_groups` (
`egid` int(10) unsigned NOT NULL,
`date` date NOT NULL,
`group_name` varchar(50) NOT NULL,
`limit` smallint(5) unsigned NOT NULL,
PRIMARY KEY (`egid`,`date`)
) ENGINE=MyISAM DEFAULT CHARSET=cp1251;
I want to extract the most recent list of groups, e.g. if a group has been changed I want to have only the last change. And I need it as a list (all groups).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
聚合函数将在 mysql 中工作,与 sql 标准不同。要从 php 访问
max(date)
的值,您必须为其指定别名:然后您可以像从 php 中选择任何其他列一样选择它,希望
有帮助
aggregate functions will work in mysql, different to the sql standard. to access the value of
max(date)
from php, you have to alias it:you can then select it like any other colum from php with
hope that helps
您的查询可能已损坏。除非满足以下两个条件之一,否则不应选择不在分组依据中的字段:
group_name
和limit
这两个字段似乎违反了这些规则。这意味着您将获得这些列的不确定结果。如果您尝试选择每组的最大值,那么您应该使用稍微不同的技术。请参阅 Quassnoi 的文章 MYSQL:为您可以使用的各种方法选择持有分组最大值的记录。
这是一种方法:
Your query might be broken. You should not select fields that aren't in the group by unless one of the following two conditions apply:
The two fields
group_name
andlimit
appear to break these rules. This means that you will get indeterminate results for these columns.If you are trying to select the max per group then you should use a slightly different technique. See Quassnoi's article MYSQL: Selecting records holding a groupwise maximum for a variety of methods you could use.
Here's one way to do it: