简体   繁体   English

PDO选择并计数“有差异”以显示在下拉菜单中

[英]PDO select and count IN HAVING DISTINCT to appear in dropdown

I have this mysql select working great. 我有这个mysql选择效果很好。 It returns the proper data. 它返回正确的数据。 I can't seem to get the context correct to place the actual count of the custnum so it will appear on the end of the dropdown option select. 我似乎无法正确地了解上下文的位置,以放置储粮库的实际数量,因此它将显示在下拉选项select的末尾。 This statement returns the proper location names 该语句返回正确的位置名称

$statement = $pdo->prepare("SELECT locationname FROM location WHERE locationname IN (SELECT locationname FROM location_user WHERE custnum= :custnum GROUP BY locationname HAVING COUNT( DISTINCT email) < 6 )");
  $statement->execute(array(':custnum' => $session->custnum));

 while($row = $statement->fetch(PDO::FETCH_ASSOC)){
echo'<option value="'.$row['locationname'].'">'.$row['locationname'].'('. $row['COUNT(total)'] .')</option>';    
}

Here's one of my attempts to grab the total for each custnum 这是我为每个保管库获取总金额的尝试之一

$statement = $pdo->prepare("SELECT locationname, COUNT(custnum) AS total FROM location WHERE  locationname IN (SELECT locationname FROM location_user WHERE custnum= :custnum GROUP BY locationname HAVING COUNT( DISTINCT email) < 6 )");
$statement->execute(array(':custnum' => $session->custnum));

while($row = $statement->fetch(PDO::FETCH_ASSOC)){
echo'<option value="'.$row['locationname'].'">'.$row['locationname'].'('. $row['total'] .')</option>';    
}

Here's my tables 这是我的桌子

       table location                            table location_user
custnum   |   locationname             custnum   |    locationname  |   email    |  userlevel
   1           location1                 1            location1       1me@you.com       3
   1           location2                 1            location1       1me@you.com       1
                                         1            location1       2me@you.com       2
                                         1            location1       3me@you.com       2
                                         1            location1       4me@you.com       2
                                         1            location1       5me@you.com       2
                                         1            location2       1me@you.com       2
                                         1            location2       1me@you.com       3

The first select returns 第一次选择返回

 location1()
 location2()

The second select returns 第二个选择返回

 location1(2)

I actually need the count of the distinct email which the query is doing and returning only the locationnames of the distinct email in the table less then 6 but how do I get the actual number of distinct emails for each locationname. 我实际上需要查询正在执行的非重复电子邮件的计数,并且仅返回表中的非重复电子邮件的位置名称(小于6),但是如何获得每个位置名称的不同电子邮件的实际数量。

This select will retrieve the total for DISTINCT email, but how do I combine the two into one for my while loop? 此选择将检索DISTINCT电子邮件的总数,但是如何在while循环中将两者合并为一个?

$statement2 = $pdo->prepare("SELECT COUNT(email) AS total FROM location_user WHERE custnum= :custnum GROUP BY locationname HAVING COUNT( DISTINCT email) < 6");
$statement2->execute(array(':custnum' => $session->custnum));

Here's the working version from the help of Peter and a little prodding from Tin. 这是在Peter的帮助下工作的版本,以及Tin的一些建议。

 $statement = $pdo->prepare("SELECT l.locationname, COUNT(DISTINCT lu.email) AS total 
 FROM location l LEFT JOIN location_user lu ON l.locationname = lu.locationname AND l.custnum = lu.custnum WHERE l.custnum = :custnum GROUP BY l.locationname HAVING COUNT(DISTINCT lu.email)  < 5 ");
 $statement->execute(array(':custnum' => $session->custnum));

 while($row = $statement->fetch(PDO::FETCH_ASSOC)){
 echo'<option value="'.$row['locationname'].'">'.$row['locationname'].'('. $row['total'] .')</option>';    
 }

Here's another version that I'm working on to skip the user that adds the locations to the table. 这是我正在尝试的另一个版本,用于跳过将位置添加到表的用户。 This user will always have a userlevel > 2. The uselevel is placed in the location_user table only as a value between 1-9. 该用户将始终具有> 2的用户级别。uselevel仅作为1-9之间的值放置在location_user表中。 So I still need the location name but I don't want their location included in the count. 因此,我仍然需要位置名称,但我不希望它们的位置包含在计数中。 I just realized that I could actually go a better route because the only email that I want to count will have a userlevel of 2. I was using the distinct email to filter out the userlevel of 1. I'll give it a go. 我只是意识到我实际上可以走一条更好的路线,因为我要计数的唯一一封电子邮件的用户级别为2。我正在使用不同的电子邮件来过滤出用户级别为1。我会努力的。 The below version drops my locations that arn't in the location_user table but it's returning the proper count. 下面的版本删除了我所在的位置,但该位置不在location_user表中,但它会返回正确的计数。

     SELECT l.locationname, COUNT(lu.userlevel) AS total 
     FROM location l LEFT JOIN location_user lu
     ON l.locationname = lu.locationname
     AND l.custnum = lu.custnum
     WHERE l.custnum = :custnum
     AND lu.userlevel = 2
     GROUP BY l.locationname
     HAVING COUNT(lu.userlevel) < 6

UPDATE2: based on your comments. UPDATE2:根据您的评论。 Try it this way 这样尝试

SELECT l.locationname, COUNT(DISTINCT lu.email) AS total 
  FROM location l LEFT JOIN location_user lu
    ON l.locationname = lu.locationname
   AND l.custnum = lu.custnum
   AND lu.userlevel < 3 -- consider only users with user level < 3
 WHERE l.custnum = ?
 GROUP BY l.locationname 
HAVING COUNT(DISTINCT lu.email) < 6

Sample output: 样本输出:

| LOCATIONNAME | TOTAL |
|--------------|-------|
|    location1 |     5 |
|    location2 |     1 |
|    location3 |     0 |

Here is SQLFiddle demo 这是SQLFiddle演示

您实际上不需要从表location查询,因为您已经从表location_user获得了locationname字段

SELECT locationname, count(DISTINCT email) as total FROM location_user WHERE custnum = :custnum GROUP BY locationname HAVING count(DISTINCT email) < 6

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

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