值= 1时的SQL计数

时间:2017-11-02 13:43:03

标签: mysql

我正在用两个表做选择:

Running virtualenv with interpreter /usr/bin/python2
New python executable in /home/kartikey/.virtualenvs/rango/bin/python2
Also creating executable in /home/kartikey/.virtualenvs/rango/bin/python
Installing setuptools, pkg_resources, pip, wheel...
  Complete output from command /home/kartikey/.virtualenvs/rango/bin/python2 - setuptools pkg_resources pip wheel:
  Collecting setuptools
Exception:
Traceback (most recent call last):
  File "/usr/share/python-wheels/pip-9.0.1-py2.py3-none-any.whl/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/share/python-wheels/pip-9.0.1-py2.py3-none-any.whl/pip/commands/install.py", line 353, in run
    wb.build(autobuilding=True)
  File "/usr/share/python-wheels/pip-9.0.1-py2.py3-none-any.whl/pip/wheel.py", line 749, in build
    self.requirement_set.prepare_files(self.finder)
  File "/usr/share/python-wheels/pip-9.0.1-py2.py3-none-any.whl/pip/req/req_set.py", line 380, in prepare_files
    ignore_dependencies=self.ignore_dependencies))
  File "/usr/share/python-wheels/pip-9.0.1-py2.py3-none-any.whl/pip/req/req_set.py", line 554, in _prepare_file
    require_hashes
  File "/usr/share/python-wheels/pip-9.0.1-py2.py3-none-any.whl/pip/req/req_install.py", line 278, in populate_link
    self.link = finder.find_requirement(self, upgrade)
  File "/usr/share/python-wheels/pip-9.0.1-py2.py3-none-any.whl/pip/index.py", line 465, in find_requirement
    all_candidates = self.find_all_candidates(req.name)
  File "/usr/share/python-wheels/pip-9.0.1-py2.py3-none-any.whl/pip/index.py", line 423, in find_all_candidates
    for page in self._get_pages(url_locations, project_name):
  File "/usr/share/python-wheels/pip-9.0.1-py2.py3-none-any.whl/pip/index.py", line 568, in _get_pages
    page = self._get_page(location)
  File "/usr/share/python-wheels/pip-9.0.1-py2.py3-none-any.whl/pip/index.py", line 683, in _get_page
    return HTMLPage.get_page(link, session=self.session)
  File "/usr/share/python-wheels/pip-9.0.1-py2.py3-none-any.whl/pip/index.py", line 792, in get_page
    "Cache-Control": "max-age=600",
  File "/home/kartikey/.virtualenvs/rango/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/sessions.py", line 501, in get
    return self.request('GET', url, **kwargs)
  File "/usr/share/python-wheels/pip-9.0.1-py2.py3-none-any.whl/pip/download.py", line 386, in request
    return super(PipSession, self).request(method, url, *args, **kwargs)
  File "/home/kartikey/.virtualenvs/rango/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/sessions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/kartikey/.virtualenvs/rango/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/sessions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "/home/kartikey/.virtualenvs/rango/share/python-wheels/CacheControl-0.11.7-py2.py3-none-any.whl/cachecontrol/adapter.py", line 47, in send
    resp = super(CacheControlAdapter, self).send(request, **kw)
  File "/home/kartikey/.virtualenvs/rango/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/adapters.py", line 423, in send
    timeout=timeout
  File "/home/kartikey/.virtualenvs/rango/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 643, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/home/kartikey/.virtualenvs/rango/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/util/retry.py", line 315, in increment
    total -= 1
TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'
----------------------------------------
...Installing setuptools, pkg_resources, pip, wheel...done.
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/virtualenv.py", line 2375, in <module>
    main()
  File "/usr/lib/python3/dist-packages/virtualenv.py", line 724, in main
    symlink=options.symlink)
  File "/usr/lib/python3/dist-packages/virtualenv.py", line 992, in create_environment
    download=download,
  File "/usr/lib/python3/dist-packages/virtualenv.py", line 922, in install_wheel
    call_subprocess(cmd, show_stdout=False, extra_env=env, stdin=SCRIPT)
  File "/usr/lib/python3/dist-packages/virtualenv.py", line 817, in call_subprocess
    % (cmd_desc, proc.returncode))
OSError: Command /home/kartikey/.virtualenvs/rango/bin/python2 - setuptools pkg_resources pip wheel failed with error code 2

我的问题是,venceu正在计算所有实例,而不仅仅是lugar = 1时的实例。为什么会这样?

尝试使用sum()也没有好结果。我该如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

如果我理解你的问题,你可以使用它:

sum(case when m.lugar = '1' then 1 else 0 end)

或者您可以尝试having子句

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);

答案 1 :(得分:1)

我很惊讶count(m.lugar ='1')语法,但确实如此,并返回与count(*)相同的语法。您可能应该将它更改为总和(lugar = 1,否则为0结束时的情况)作为venceu。您还应密切关注该组,以确保它按预期工作(我怀疑不是)。

答案 2 :(得分:1)

count(x)不接受表达。

它只计算返回x的次数。

你应该做的是检查m.lugar是否为1,然后在计数器中添加一个,否则什么都不做。

内联检查可以这样完成:

case when m.lugar = '1' then 1 else 0 end

然后添加你得到的所有内容:

sum(case when m.lugar = '1' then 1 else 0 end)

您的最终查询应如下所示:

SELECT 
    m.torneio, 
    m.deck, 
    m.top, 
    m.lugar, 
    sum( m.quantidade ) AS quantidade, 
    m.formato AS formato, 
    q.quantidade AS qtorneio, 
    t.season AS season, 
    sum( m.top ) AS totaltops, 
    sum(case when m.lugar = '1' then 1 else 0 end) AS venceu
FROM 
    `metagame` AS m, 
    quantidade AS q, 
    torneios AS t
WHERE 
    m.torneio = t.nome
    AND m.torneio = q.nome
GROUP BY 
    m.deck