MySql Count()----如何 [英] MySql Count() ----How To

查看:69
本文介绍了MySql Count()----如何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法获得此查询来计算数据库中实例的数量.我认为我的语法有误.

I can''t seem to get this query to count the number of instances from the database. I think my syntax is wrong.

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>
#include <windows.h>
#include <winsock.h>
#include <c:\\mysql\\include\\mysql.h>

using namespace std;

int main()
   {
   MYSQL* conn;
   
   conn = mysql_init(NULL);
     if (mysql_real_connect(conn,"urlock.db.5513143.hostedresource.com","VendorCheck","Reader1234","urlock",0,NULL,0) !=0)
     {
		cout << "Connected!" <<endl;
		query << "SELECT COUNT(173.201.216.2) AS row_count FROM urlock";
        StoreQueryResult bres = query.store();
        cout << "Total rows: " << bres[0]["row_count"];

	 }
	 else
	 {
        cout << "Failed!" <<endl;
	 }
	 mysql_close(conn);
     system("PAUSE"); 
     return 0;
   }</winsock.h></windows.h></fstream></conio.h></string></iostream></stdio.h>

推荐答案

也许,如果给它一个列引用或"*"而不是IP地址,可能会有所帮助...
Perhaps, if you gave it a column reference, or ''*'', rather than an IP address, it might help...
SELECT COUNT(myColumn) AS row_count FROM urlock


还是您要计算该IP地址的行数?


Or are you trying to count how many rows there are with that IP address?

SELECT COUNT(myColumn) AS row_count FROM urlock WHERE myColumnWithIPAddress='173.201.216.2'


CString sSql;
sSql.Format("SELECT COUNT(*) as mycount FROM Series WHERE Name = ''jim''") ;

recordset.Open(AFX_DAO_USE_DEFAULT_TYPE, sSql, NULL);

long count = recordset.GetFieldValue("mycount").lVal;



现在,变量计数存储与您作为整数提供的条件进行的匹配次数(在名称"字段中有jim的记录数).


上面的查询与MS Access完美配合.我不确定您拥有哪个数据库.希望以上示例可以帮助您在程序中继续前进.您可以使用此代码,也可以即兴使用它来与数据库和代码一起使用.

我在Google上搜索时发现以下查询.希望这对您有帮助:-



Now the variable count stores how many matches were made with the criteria you supply as an integer(how many records have jim in the field "name").


The above query works perfectly fine with MS Access. I am not sure what database you have. I hope the above example helps you to move forward in your program. You can use this code and improvise it to work with your database and code.

I found the following query while i was searching on google. Hope this helps you: -

SQL / MySQL » Select Clause » Count	 	
	
COUNT command with condition

/*
mysql>  SELECT species, sex, COUNT(*) FROM Bird
    ->         WHERE species = ''dog'' OR species = ''cat''
    ->         GROUP BY species, sex;
Empty 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);

/* You need not retrieve an entire table when you use COUNT(). 
For example, the previous query, when performed just on dogs and 
cats, looks like this:
*/
 SELECT species, sex, COUNT(*) FROM Bird
        WHERE species = ''dog'' OR species = ''cat''
        GROUP BY species, sex;



这段代码确实可以为您提供帮助.如果您不需要删除该组,而只需删除它,而不必具有两个条件,则保持条件不变,我相信这会对您的代码有所帮助.

让我知道是否有任何代码对您有所帮助.

-
AJ



This code should really help you. If you don''t need the group by just remove it and instead of having two conditions, just keep on condition and I am sure this will help you in your code.

Let me know if any of the codes help you.

--
AJ


这篇关于MySql Count()----如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆