显示本月在特定城市尚未创建项目的用户

时间:2019-05-23 14:18:04

标签: mysql sql

我有这个查询,用于显示本月尚未创建项目的客户。

SELECT u.userName
FROM (SELECT DISTINCT userName FROM projects) u
LEFT JOIN projects p
    ON u.userName = p.userName AND
       MONTH(p.date) = MONTH(CURRENT_DATE()) AND
       YEAR(p.date) = YEAR(CURRENT_DATE())
WHERE
    p.userName IS NULL

实时示例:http://sqlfiddle.com/#!9/53de526/5

现在我想按城市过滤它们。我尝试了不同的方法,但没有一个起作用。

有什么想法吗?

谢谢

5 个答案:

答案 0 :(得分:1)

从您的问题和示例中,我想到了这个……尝试一下,看看您想要什么。

ans = str
gram = 20
bprice = int
sprice = int
totalg = 20
balance = 10
totalc = 20
timesb = 0
timess = int
profit = balance - totalc

while ans != "buy" and ans!="sell" and ans!="enter" and ans!="check":
     print ("Would you like to Buy, Sell, Enter Variables or Check Records?")
     ans = input()
if ans == "buy" :
     print ("How many grams?")
     gram = input()
     print ("How much did you pay for it?")
     bprice = input()
     totalg = totalg + int(gram)
     balance = balance - int(bprice)
     totalc = int(totalc) + int(bprice)
     timesb = timesb + 1

答案 1 :(得分:1)

有很多方法可以实现这一目标。这是一个:

SELECT
  p.*
FROM
  projects AS p
  INNER JOIN
  (
    SELECT
      userName
      ,MAX(date) AS max_date
    FROM 
      projects p
    GROUP BY
        userName
    HAVING
      max_date < DATE_FORMAT(CURRENT_DATE, '%Y-%m-01')
  ) AS mp
  ON p.userName = mp.userName AND p.date = mp.max_date
WHERE
  p.city = 'Bordeaux'

这将找到date的最大值,对当月未发生的值进行过滤,然后将结果结合回到原始表中,然后对city进行过滤。我以'Bordeaux'为例,但是您可以输入所需的任何值。

答案 2 :(得分:0)

SELECT u.userName, u.city
        FROM (SELECT DISTINCT * FROM projects) u
        LEFT JOIN projects p
            ON u.userName = p.userName AND
               MONTH(p.date) = MONTH(CURRENT_DATE()) AND
               YEAR(p.date) = YEAR(CURRENT_DATE())      
        WHERE
            p.userName IS NULL AND u.city='Paris'

答案 3 :(得分:0)

规格尚不清楚。

我们可以生成(用户名,城市)对,当月没有匹配的项目。如果我们没有城市值列表的来源,则可以从projects表中获取它,就像我们获得了用户名列表一样:

SELECT u.username
     , c.city
  FROM ( SELECT pu.username
           FROM projects pu
          GROUP
             BY pu.username
       ) u
 CROSS
  JOIN ( SELECT pc.city
           FROM projects pc
          GROUP
             BY pc.city
       ) c
    -- anti-join    
  LEFT
  JOIN projects p
    ON p.username = u.username
   AND p.city     = c.city
   AND p.date     >= DATE_FORMAT(NOW(),'%Y-%m-01') + INTERVAL 0 MONTH
   AND p.date     <  DATE_FORMAT(NOW(),'%Y-%m-01') + INTERVAL 1 MONTH
 WHERE p.username IS NULL
 ORDER
    BY u.username
     , c.city

如果要“过滤”,可以添加到WHERE子句中...

 WHERE p.username IS NULL
   AND c.city = 'Paris'

或者我们可以将该条件纳入内联视图c

SELECT u.username
     , c.city
  FROM ( SELECT pu.username
           FROM projects pu
          GROUP
             BY pu.username
       ) u
 CROSS
  JOIN ( SELECT pc.city
           FROM projects pc
          WHERE pc.city = 'Paris'
          GROUP
             BY pc.city
       ) c
    -- anti-join    
  LEFT
  JOIN projects p
    ON p.username = u.username
   AND p.city     = c.city
   AND p.date     >= DATE_FORMAT(NOW(),'%Y-%m-01') + INTERVAL 0 MONTH
   AND p.date     <  DATE_FORMAT(NOW(),'%Y-%m-01') + INTERVAL 1 MONTH
 WHERE p.username IS NULL
 ORDER
    BY u.username
     , c.city

答案 4 :(得分:0)

只需使用聚合:

select p.username
from projects p
group by p.username
having sum( month(p.date) = month(CURRENT_DATE()) and
            year(p.date) = year(CURRENT_DATE()) and
          ) = 0;

您提到这座城市。看来您的数据已被解密,并且city是给用户的,而不是 project 的。如果是这样,只需将其放在selectgroup by中:

select p.username, p.city
from projects p
group by p.username, p.city
having sum( month(p.date) = month(CURRENT_DATE()) and
            year(p.date) = year(CURRENT_DATE()) and
          ) = 0;

通常,您将有一个单独的用户表。上面的内容显然只会返回拥有项目的用户。但是,使用单独的表,您还可以使用户拥有 no 项目。为此,我将使用not exists

select u.*
from users u
where not exists (select 1
                  from projects p
                  where month(p.date) = month(CURRENT_DATE()) and
                        year(p.date) = year(CURRENT_DATE())
                 );

您将获得该城市以及其他信息。