使用 Subsonic 3 进行 GroupBy 计数
我喜欢使用 SubSonic 3 执行以下操作,而不使用 Linq(使用列和表的动态查找)。另外,没有内联 sql(以防止 sql 注入)。
http://www.java2s.com/Code/SQL/Select-子句/COUNTandGROUPBY.htm
mysql> SELECT species, COUNT(*) FROM Bird GROUP BY species;
+---------+----------+
| species | COUNT(*) |
+---------+----------+
| Bus | 2 |
| Car | 1 |
+---------+----------+
2 rows in set (0.00 sec)
Drop table Bird;
CREATE TABLE Bird (
name VARCHAR(20),
owner VARCHAR(20),
species VARCHAR(20),
sex CHAR(1),
birth DATE,
death DATE
);
INSERT INTO Bird VALUES ('BlueBird','Joe','Car','f','1999-03-30',NULL);
INSERT INTO Bird VALUES ('RedBird','Yin','Bus','m','1979-04-30',1998-01-30);
INSERT INTO Bird VALUES ('RedBird','Yin','Bus','m','1998-01-30',NULL);
/* COUNT() and GROUP BY
Number of animals per species:
*/
SELECT species, COUNT(*) FROM Bird GROUP BY species;
I like to perform the following with SubSonic 3 without using Linq (using dynamic lookup of columns and tables). Also, no inline sql (to prevent sql-injections).
http://www.java2s.com/Code/SQL/Select-Clause/COUNTandGROUPBY.htm
mysql> SELECT species, COUNT(*) FROM Bird GROUP BY species;
+---------+----------+
| species | COUNT(*) |
+---------+----------+
| Bus | 2 |
| Car | 1 |
+---------+----------+
2 rows in set (0.00 sec)
Drop table Bird;
CREATE TABLE Bird (
name VARCHAR(20),
owner VARCHAR(20),
species VARCHAR(20),
sex CHAR(1),
birth DATE,
death DATE
);
INSERT INTO Bird VALUES ('BlueBird','Joe','Car','f','1999-03-30',NULL);
INSERT INTO Bird VALUES ('RedBird','Yin','Bus','m','1979-04-30',1998-01-30);
INSERT INTO Bird VALUES ('RedBird','Yin','Bus','m','1998-01-30',NULL);
/* COUNT() and GROUP BY
Number of animals per species:
*/
SELECT species, COUNT(*) FROM Bird GROUP BY species;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你几乎做到了:
试试这个:
这在 SQL 中工作,我认为它在 MySql 中工作
You almost did it:
Try This:
This Work in SQL and i think it works in MySql