我本周早些时候提出了一个问题,我得到了这个陈述作为答案:
select published, count(*) nbr
from table1
group by published
order by nbr desc
limit 1
我现在想知道如何在语句中添加where子句,以便限制我得到的不同类型的出版物的结果。我的表看起来像这样:
where子句将在type列上,例如type=3
。
提前致谢
迪安
答案 0 :(得分:5)
是时候开始学习一些基本的SQL了,你不觉得吗? :)
http://dev.mysql.com/doc/refman/4.1/en/select.html
SELECT
[ALL | DISTINCT | DISTINCTROW ]
[HIGH_PRIORITY]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
[SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
select_expr [, select_expr ...]
[FROM table_references
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[PROCEDURE procedure_name(argument_list)]
[INTO OUTFILE 'file_name' export_options
| INTO DUMPFILE 'file_name'
| INTO @var_name [, @var_name]]
[FOR UPDATE | LOCK IN SHARE MODE]]
答案 1 :(得分:3)
当然,只需在WHERE
声明之后和FROM
声明之前添加GROUP BY
声明。
答案 2 :(得分:2)
select published, count(*) nbr
from table1
where type = 3
group by published
order by nbr desc
limit 1
答案 3 :(得分:2)
就基本规则而言,请参阅where子句将约束放在每一行上。行级约束首先创建一个表,在该表上进行分组。所以集团来自Where。如果你想要组级约束,你可能会选择Having子句,但是记住一件事,你可以只对组中常见的项(在你的情况下是已发布的)中应用约束。
如前所述,您案件中的Where子句将放在“group by”之前和“from table1”之后