我有一个标签表,想从列表中获取最高计数标签.
示例数据如下所示
id (1) tag ('night')
id (2) tag ('awesome')
id (3) tag ('night')
Run Code Online (Sandbox Code Playgroud)
运用
SELECT COUNT(*), `Tag` from `images-tags`
GROUP BY `Tag`
Run Code Online (Sandbox Code Playgroud)
让我找回我正在寻找的数据.但是,我想组织它,以便最高标签计数是第一个,并限制它只发送给我前20个左右.
我试过这个......
SELECT COUNT(id), `Tag` from `images-tags`
GROUP BY `Tag`
ORDER BY COUNT(id) DESC
LIMIT 20
Run Code Online (Sandbox Code Playgroud)
而且我一直在"无效使用群组功能 - ErrNr 1111"
我究竟做错了什么?
我正在使用MySQL 4.1.25-Debian
我正在使用MySQL.这是我的架构:
供应商(sid:整数,sname:字符串,地址字符串)
零件(pid:整数,pname:字符串,颜色:字符串)
目录(sid:整数,pid:整数,成本:实际)
(主键是粗体)
我正在尝试编写一个查询来选择至少由两个供应商制作的所有零件:
-- Find the pids of parts supplied by at least two different suppliers.
SELECT c1.pid -- select the pid
FROM Catalog AS c1 -- from the Catalog table
WHERE c1.pid IN ( -- where that pid is in the set:
SELECT c2.pid -- of pids
FROM Catalog AS c2 -- from catalog
WHERE c2.pid = c1.pid AND COUNT(c2.sid) >= 2 -- where there are at least two corresponding …
Run Code Online (Sandbox Code Playgroud) 大家好我有以下表格设置:
Articles:
ID | TITLE | CONTENT | USER | NUM_COMMENTS
COMMENTS
ID | ARTICLE_ID | TEXT
Run Code Online (Sandbox Code Playgroud)
我需要一个sql语句来更新articles表的NUM_Comments字段,其中包含针对文章的评论计数,例如:
update articles a, comments f
set a.num_comments = COUNT(f.`id`)
where f.article_id = a.id
Run Code Online (Sandbox Code Playgroud)
上面的sql不起作用,我得到组函数无效的使用错误.我在这里使用MySQL.
这样可行:
SELECT c.name AS country_name, c.population AS country_population, SUM(ci.population) AS city_population, ROUND(100*(SUM(ci.population)/c.population)) AS city_population_percent
FROM country AS c
JOIN city AS ci
ON c.code = ci.countrycode
WHERE c.continent = 'Europe'
GROUP BY c.name
Run Code Online (Sandbox Code Playgroud)
但是我只需要获取大于30的city_population_percent值,所以我试试这个:
SELECT c.name AS country_name, c.population AS country_population, SUM(ci.population) AS city_population, ROUND(100*(SUM(ci.population)/c.population)) AS city_population_percent
FROM country AS c
JOIN city AS ci
ON c.code = ci.countrycode
WHERE c.continent = 'Europe'
**AND ROUND(100*(SUM(ci.population)/c.population)) > 30**
GROUP BY c.name
Run Code Online (Sandbox Code Playgroud)
那是我得到的时候:
错误代码1111.无效使用组功能
也就是说,当我在以下情况中添加此条件时,它会失败WHERE
:
AND ROUND(100*(SUM(ci.population)/c.population)) > 30
Run Code Online (Sandbox Code Playgroud) 我想做的是:
UPDATE table SET field = MAX(field) + 1 WHERE id IN (1, 3, 5, 6, 8);
Run Code Online (Sandbox Code Playgroud)
在我看来,这个语句的语义首先是数据库会出现并确定我field
所有的最大值是什么table
.然后它会将1加到该值,并将结果值分配给field
具有id
1,3,5,6和8 的行的列.看起来很简单......
当我尝试运行该查询时,MySQL会对它进行扼杀并说:
ERROR 1111 (HY000): Invalid use of group function
Run Code Online (Sandbox Code Playgroud)
为了得到我想要的结果,你必须使用什么秘诀?
此致,维克
我有这个相当复杂的查询,从三个表中获取数据,现在我希望它更复杂(哦,亲爱的)!
我希望最后发布的功能显示在页面的自己的部分,通过选择表格中的最后一个条目,这很容易.但是,对于复杂查询(网站的主页),我希望不能显示此功能.
我想对union
以前的查询进行以下查询,但它没有返回正确的结果:
SELECT
features.featureTitle AS title,
features.featureSummary AS body,
features.postedOn AS dummy,
DATE_FORMAT( features.postedOn, '%M %d, %Y' ) AS posted,
NULL,
NULL,
staff.staffName,
features.featureID
FROM
features
LEFT JOIN staff ON
features.staffID = staff.staffID
WHERE features.postedOn != MAX(features.postedOn)
ORDER BY dummy DESC LIMIT 0,15
Run Code Online (Sandbox Code Playgroud)
此查询返回以下错误:
MySQL错误:#1111 - 无效使用组功能
有没有办法解决?
我的rails应用程序中有两个模型.项目和评论.评论属于Item和Items有很多评论.
审核模型如下所示:
create_table "reviews", :force => true do |t|
t.text "comment"
t.integer "rating"
t.integer "reviewable_id"
t.string "reviewable_type"
t.datetime "created_at"
t.datetime "updated_at"
end
Run Code Online (Sandbox Code Playgroud)
(评论是多态的,这就是为什么他们有reviewable_id和reviewable_type)
我正在尝试将ActiveRecord查询放在一起,这将允许我选择平均评级为80或更高的所有项目.
我已经尝试了许多不同的变化,我认为这些变化有效
Item.joins(:reviews).where("avg(reviews.rating) > 80").group(:id)
Run Code Online (Sandbox Code Playgroud)
但是这会导致以下错误:
Error: Mysql2::Error: Invalid use of group function: SELECT `items`.* FROM `items` INNER JOIN `reviews` ON `reviews`.`reviewable_id` = `items`.`id` AND `reviews`.`reviewable_type` = 'Item' WHERE (avg(reviews.rating) > 80) GROUP BY id
Run Code Online (Sandbox Code Playgroud)
如果有人可以提供帮助,我将不胜感激.
这可能吗?
我有2个表,客户和订单.现在,我想在客户中填写该客户的所有订单ID(以逗号分隔).
我试过这样的东西,但它不起作用:
UPDATE customers AS c
LEFT JOIN orders AS o ON o.customerid=c.customerid
SET c.orders = GROUP_CONCAT(DISTINCT o.orderid)
Run Code Online (Sandbox Code Playgroud)
我得到'无效使用群组功能'.
PS.我知道总是在SELECT/JOIN中动态获取GROUP_CONCAT值会更好,但我只是想知道我是否可以用某种方式填充这个列.
这是我正在使用的查询:
SELECT k_id, COUNT(k_id) AS k_count
FROM template_keyword_link
WHERE k_id IN(1,2,3,4,5)
GROUP BY k_id;
Run Code Online (Sandbox Code Playgroud)
此查询返回类似
1 |的内容 6
2 | 1
3 | 4
4 | 1
5 | 9
我想添加类似的东西AND COUNT(k_id) = 1
我最终得到
2 | 1
4 | 1
但是我无效使用组功能.
我该怎么做呢?
我的问题的另一部分是.
这可以用作删除语句吗?
就像是
DELETE FROM
template_keyword_link tkl
LEFT JOIN keywords k
ON tkl.k_id = k.k_id
WHERE tkl.k_id
IN(SELECT k_id, COUNT(k_id) AS k_count
FROM template_keyword_link
WHERE k_id IN(1,2)
GROUP BY k_id
HAVING k_count = 1);
Run Code Online (Sandbox Code Playgroud)
我明白了 …
我有两个型号,Post
hasMany Comment
.如何选择Post
少于两个的所有Comment
?
我试着用find
用'fields'=>array('COUNT(Comment.id) as numComments','Post.*')
,(然后做numComments < 2
的'conditions'
).但是,我收到了一个Unknown column 'Comment.id' in 'field list'
错误.
谢谢!
编辑:我已经得到CakePHP来生成这个查询:
SELECT `Post`.*, FROM `posts` AS `Post`
LEFT JOIN comments AS `Comment` ON (`Post`.`id` = `Comment`.`text_request_id`)
WHERE COUNT(`Comment`.`id`) < 2
GROUP BY `Comment`.`post_id`
LIMIT 10
Run Code Online (Sandbox Code Playgroud)
但是我#1111 - Invalid use of group function
的COUNT
功能出错了.
编辑:已解决,使用HAVING COUNT而不是WHERE COUNT.
mysql-error-1111 ×10
mysql ×9
sql ×7
activerecord ×1
cakephp ×1
cakephp-1.3 ×1
group-concat ×1
join ×1
sql-delete ×1