为什么你需要把你自己创建的(例如列select 1 as "number"
后)HAVING
,而不是WHERE
在MySQL?
是否有任何缺点而不是做WHERE 1
(编写整个定义而不是列名)?
我需要在WHERE子句中使用别名,但它一直告诉我它是一个未知的列.有什么方法可以解决这个问题吗?我需要选择评级高于x的记录.评级计算如下:
sum(reviews.rev_rating)/count(reviews.rev_id) as avg_rating
Run Code Online (Sandbox Code Playgroud) 假设我有这张桌子
id | cash
1 200
2 301
3 101
4 700
Run Code Online (Sandbox Code Playgroud)
我想要返回所有先前现金总和大于某个值的第一行:
因此,例如,如果我想要返回所有先前现金总和大于500的第一行,则应返回第3行
我如何使用mysql语句执行此操作?
使用WHERE SUM(cash) > 500
不起作用
对于SQL中的这个查询(查找重复项):
SELECT userId, name FROM col GROUP BY userId, name HAVING COUNT(*)>1
Run Code Online (Sandbox Code Playgroud)
我在MongoDB中执行了这个简单的查询:
res = db.col.group({key:{userId:true,name:true},
reduce: function(obj,prev) {prev.count++;},
initial: {count:0}})
Run Code Online (Sandbox Code Playgroud)
我添加了一个简单的Javascript循环来遍历结果集,并执行了一个过滤器来查找count> 1的所有字段,如下所示:
for (i in res) {if (res[i].count>1) printjson(res[i])};
Run Code Online (Sandbox Code Playgroud)
除了在客户端使用javascript代码之外,还有更好的方法吗?如果这是最好/最简单的方式,说它是,这个问题将有助于某人:)
我需要在sakila数据库中找到电影的最长租期.我试过这个:
SELECT DISTINCT
customer.first_name
FROM
rental,
customer
WHERE
rental.customer_id = customer.customer_id
GROUP BY
rental.rental_id
HAVING
(
rental.return_date - rental.rental_date
) =(
SELECT
MAX(countRental)
FROM
(
SELECT
(
rental.return_date - rental.rental_date
) AS countRental
FROM
rental,
customer
GROUP BY
rental.rental_id
) AS t1
)
Run Code Online (Sandbox Code Playgroud)
但我收到错误:
有谁知道为什么?我使用了一个应该是聚合数据的列..我缺少什么
有一个带有item_id和color_id的product表.我正在尝试使用大多数非null实例获取color_id.
这失败了:
SELECT color_id
FROM products
WHERE item_id=1234
GROUP BY item_id
HAVING MAX(COUNT(color_id))
Run Code Online (Sandbox Code Playgroud)
同
Invalid use of group function
Run Code Online (Sandbox Code Playgroud)
这个
SELECT color_id, COUNT(color_id)
FROM products
WHERE item_id=1234
GROUP BY item_id
Run Code Online (Sandbox Code Playgroud)
返回
color_id count
1, 323
2, 122
3, 554
Run Code Online (Sandbox Code Playgroud)
我正在寻找具有最多实例的color_id 3.
有没有2个查询可以快速简便地获得我想要的东西?
以下两个查询产生完全相同的结果:
select country, count(organization) as N
from ismember
group by country
having N > 50;
select * from (
select country, count(organization) as N
from ismember
group by country) x
where N > 50;
Run Code Online (Sandbox Code Playgroud)
每个HAVING
子句都可以用子查询和这样的WHERE
子句替换吗?或者是否存在一个HAVING
条款绝对必要/更强大/更有效/无论什么情况?
我需要创建一个查询,我需要COUNT(*)
和HAVING COUNT(*) = x
.
我正在使用一个使用CustomProjection
该类的工作,我在某处下载了.
这是我尝试实现的SQL:
select count(*) as y0_, this_.ensayo_id as y1_ from Repeticiones this_
inner join Lineas linea1_ on this_.linea_id=linea1_.id
where this_.pesoKGHA>0.0 and this_.nroRepeticion=1 and linea1_.id in (18,24)
group by this_.ensayo_id
having count(*) = 2
Run Code Online (Sandbox Code Playgroud)
这是我使用Projection
Hibernate类的代码:
critRepeticion.setProjection(Projections.projectionList()
.add( Projections.groupProperty("ensayo") )
.add( CustomProjections.groupByHaving("ensayo_id",Hibernate.LONG,"COUNT(ensayo_id) = "+String.valueOf(lineas.size()))
.add( Projections.rowCount() )
);
Run Code Online (Sandbox Code Playgroud)
错误是:
!STACK 0
java.lang.NullPointerException
at org.hibernate.criterion.ProjectionList.toSqlString(ProjectionList.java:50)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getSelect(CriteriaQueryTranslator.java:310)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:71)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:67)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1550)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
at ar.com.cse.cseagro.controller.RepeticionController.buscarEnsayo(RepeticionController.java:101)
Run Code Online (Sandbox Code Playgroud)
如果我用CustomProjections
类注释该行,查询工作,但我没有得到HAVING …
我知道这个问题已经讨论得很多了,但是我的研究都没有让我相信MySQL中的' where
'和' having
'条款之间的区别.根据我的理解,我们可以使用' having
' 来实现'where'子句可以完成的所有事情.例如.select * from users having username='admin'
.那为什么你需要' where
'条款?使用哪里可以产生任何性能差异?
我有这样的桌子
Table1
ID | Val | Val2 |
606541 |3175031503131004|3175032612900004|
606542 |3175031503131004|3175032612900004|
677315 |3175031503131004|3175032612980004|
222222 |1111111111111111|8888888888888888|
231233 |1111111111111111|3175032612900004|
111111 |9999992222211111|1111111111111111|
57 |3173012102121018|3173015101870020|
59 |3173012102121018|3173021107460002|
2 |900 |7000 |
4 |900 |7001 |
Run Code Online (Sandbox Code Playgroud)
我有两个条件列Val
和Val2
。显示结果,如果Val
:
例如 :
示例 1
ID | Val | Val2 |
606541 |3175031503131004|3175032612900004|
606542 |3175031503131004|3175032612900004|
677315 |3175031503131004|3175032612980004|
False, because even the Val column
had two or more duplicate but the Val2
had dulicate value (ID 606541 and …
Run Code Online (Sandbox Code Playgroud)