检查表中较早的特定ID行是否出现

时间:2016-02-26 14:36:03

标签: php mysql sql

我有一张客户订单表,如下所示:

order_id | customer_id |    date    | ...
-----------------------------------------
   583         192       2015-05-01   ...
   734         143       2015-06-04   ...
   801         455       2015-07-02   ...
   ...         ...           ...      ...

我想知道在一个月内收到的新客户数。通过查询上面的订单表找到新客户,并找到该月出现但未在该月之前出现的customer_id值的数量(在该月之后很好)。我正在寻找新customer_id的第一个实例。

有没有办法在一个SQL语句中执行此操作?我可以做以下事情,我想:

  • 通过使用order_id
  • 查询数据库,从一个月内获取所有DISTINCT customer_idLIKE YYYY-MM-%
  • 检查表格中customer_id的实例,order_id更小(原因是较小的order_id将是较早的订单)
  • 如果找到,请跳过
  • 如果找不到,请递增一个计数器

3 个答案:

答案 0 :(得分:1)

假设python manage.py migrate Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line utility.execute() File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 327, in execute django.setup() File "C:\Python27\lib\site-packages\django\__init__.py", line 18, in setup apps.populate(settings.INSTALLED_APPS) File "C:\Python27\lib\site-packages\django\apps\registry.py", line 108, in populate app_config.import_models(all_models) File "C:\Python27\lib\site-packages\django\apps\config.py", line 202, in import_models self.models_module = import_module(models_module_name) File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module __import__(name) File "C:\Python27\lib\site-packages\django\contrib\auth\models.py", line 4, in <module> from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager File "C:\Python27\lib\site-packages\django\contrib\auth\base_user.py", line 49, in <module> class AbstractBaseUser(models.Model): File "C:\Python27\lib\site-packages\django\db\models\base.py", line 108, in __new__ new_class.add_to_class('_meta', Options(meta, app_label)) File "C:\Python27\lib\site-packages\django\db\models\base.py", line 299, in add_to_class value.contribute_to_class(cls, name) File "C:\Python27\lib\site-packages\django\db\models\options.py", line 263, in contribute_to_class self.db_table = truncate_name(self.db_table, connection.ops.max_name_length()) File "C:\Python27\lib\site-packages\django\db\__init__.py", line 36, in __getattr__ return getattr(connections[DEFAULT_DB_ALIAS], item) File "C:\Python27\lib\site-packages\django\db\utils.py", line 212, in __getitem__ backend = load_backend(db['ENGINE']) File "C:\Python27\lib\site-packages\django\db\utils.py", line 135, in load_backend raise ImproperlyConfigured(error_msg) django.core.exceptions.ImproperlyConfigured: 'django_riak_engine.riak' isn't an available database backend. Try using 'django.db.backends.XXX', where XXX is one of: 'mysql', 'oracle', 'postgresql', 'sqlite3' Error was: No module named riak.base 是您的期间开始日期,date_From是期​​末日期,您可以这样做:

date_To

此处使用相同的select distinct T1.customer_id from your_table as T1 left outer join your_Table as T2 on T1.customer_id = T2.customer_id and T2.date < date_From where T1.date <= date_To and T1.date >= date_From and T2.customer_id is null 和您的期间之前的日期加入同一个表,并检查该加入没有结果(customer_id),确保您的T2.customer_id is null首先按照以下顺序显示你的时期而不是更早。

答案 1 :(得分:1)

这将在2016年1月列出您的新客户

select customer_id,order_id from customer c 
where MONTHNAME(date) = 'January' and YEAR(date) = 2016
    and date >= 
(select min(date) mindate from customer c1 
where c.customer_id = c1.customer_id 
having MONTH(c.date) <> MONTH(c1.mindate) or YEAR(c.date) <> YEAR(c1.
mindate));

答案 2 :(得分:1)

如果您需要计算客户数量,那么您不需要order_id。您只需要customer_id和date:

select count(*) from 
    ( select min(date) as first_contact, customer_id 
      from the_table 
      group by customer_id )  new_customers
where  new_customers.first_contact > start_of_the_month 
   and new_customers.first_contact < end_of_the_month