简体   繁体   English

从mysql_num_rows结果获取计数

[英]Get count from mysql_num_rows result

I have a form that collects RSVPs for a party. 我有一个收集聚会的RSVP的表格。 The person filling out the form (Name) has an option to bring a guest (Guest). 填写表格(姓名)的人可以带客人(客人)。 In the notification email, I am trying to give an accurate count of RSVPs with total Name and Guest counts. 在通知电子邮件中,我试图提供准确的RSVP数量以及姓名和访客总数。

It is easy to do if I was just getting the Name count (for person filling out the form): 如果我只是获得姓名计数(对于填写表格的人),这很容易做到:

mysql_query($query);
$result = mysql_query("select * from tablename"); 
$rowCount = mysql_num_rows($result); 
mysql_close($link);

and I would simply use $rowCount as the total number of RSVPs. 而我只是将$rowCount用作$rowCount的总数。 But I also want to include Guests in the count. 但我也想将来宾包括在内。 The Guest is entered as GuestFirstName and GuestLastName in the form and goes on the same record as the person filling out the form. 该访客在表单中输入为GuestFirstNameGuestLastName ,并与填写表单的人记录在同一记录上。 So I tried this. 所以我尝试了这个。 It worked when I included a guest - it said total RSVPs were 2. But when I did not include a guest on the next test I did, it also counted it as 2. Here is the code I used for that: 当我包括一个来宾时,它起作用了-它说总的RSVP为2。但是当我在下一个测试中不包括来宾时,它也算为2。这是我使用的代码:

mysql_query($query);
$result = mysql_query("select * from tablename"); 
$result_guest = mysql_query("select * from tablename WHERE 
GuestFirstName IS NOT NULL); 
$rowCount = mysql_num_rows($result) + mysql_num_rows($result_guest) ; 
mysql_close($link);    

Like I said, this code yields 2 RSVPs even when no guest is entered. 就像我说的那样,即使没有输入来宾,此代码也会产生2个RSVP。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks. 谢谢。

cdr6800 cdr6800

and

You can achieve this in a single query like the following. 您可以通过以下单个查询来实现。 Also try to use PDO or mysqli_* functions because you use mysql_* which are deprecated. 也请尝试使用PDO或mysqli_ *函数,因为您已弃用了mysql_ *。

SELECT 
    COUNT(*) AS total_names,
    COUNT(CASE WHEN GuestFirstName IS NOT NULL THEN 1 END) AS total_guests
FROM tablename;

So, if there are 10 invitation rows and only 4 of them have guests results will be: 因此,如果有10条邀请行,而其中只有4条邀请人,结果将是:

total_names    total_guests
10             4

That's an expensive way to get a count of rows: selecting all columns from all rows in the table, and fetching those from the database to the client, to get just a "count" of the number of rows. 这是一种获取行数的昂贵方法:从表中的所有行中选择所有列,然后从数据库中将这些列提取到客户端,以仅获取行数的“计数”。

SQL provides aggregate functions. SQL提供聚合函数。 For example COUNT() can be used to return a count of rows. 例如, COUNT()可用于返回行数。


As far as why you are getting two rows returned... 至于为什么要返回两行...

  WHERE GuestFirstName IS NOT NULL

The most likely explanation is that there are two rows in the table which have a non-NULL value assigned to the GuestFirstName column. 最可能的解释是,表中有两行具有分配给GuestFirstName列的非NULL值。 It may be an empty (zero-length string), which isn't considered NULL. 它可能是空的(零长度字符串),不认为是NULL。 Consider 考虑

  SELECT '' IS NULL       -- 0 (false)
       , '' IS NOT NULL   -- 1 (true)     

I think you probably also want to exclude from the count rows where the GuestFirstName is equal to the empty string. 我认为您可能还想从GuestFirstName等于空字符串的计数行中排除。


The PHP mysql_ extension is deprecated , and will be removed in future release. 不建议使用 PHP mysql_扩展名,并将在以后的版本中将其删除。 Two suitable replacements are available: PDO and mysqli . 有两个合适的替代品: PDOmysqli


And use a query that returns a single row with a count of rows. 并使用返回单行和行数的查询。 That will be more efficient than returning the entire table, just to get a count. 这将比返回整个表更有效,只是为了计数。

And do it in one pass through the data. 并一次通过数据。 For a row that includes a Guest, increment the count by 2, otherwise increment the count by 1. Use a SUM() aggregate, with an expression that returns either 2 or 1. 对于包含来宾的行,将计数增加2,否则将计数增加1。使用SUM()聚合,其表达式返回2或1。

For example: 例如:

SELECT SUM(IF(t.GuestFirstName<>'',2,1)) AS total_including_guests
     , SUM(1) AS count_rsvp
  FROM mytable t

Maybe Guest Name has an empty (but not null) value. 来宾名称可能为空(但不为null)。 Try: 尝试:

$result_guest = mysql_query( select * from tablename 
    WHERE GuestFirstName IS NOT NULL 
    AND GuestFirstName != '');

You can actually do this with one query and no additional logic, using SUM() 实际上,您可以使用SUM()进行一个查询而无需附加逻辑来执行此操作

SELECT SUM(IF(GuestFirstName IS NULL, 1, 2)) AS totalGuests FROM tablename;

This will iterate over all rows and and sum up 1 (if 1 guest in this row is coming) or 2 (if "main" guest + his invite are coming) 这将遍历所有行,并总计1(如果该行中有1位客人来了)或2(如果“主要”客人+他的邀请来了)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM