简体   繁体   English

mysql SELECT列但只使用WHERE

[英]mysql SELECT columns but use WHERE to only one

I have this query: 我有这个问题:

SELECT  i.d, COUNT(id) AS dr, COUNT(id2) AS dn, SUM(eq) AS eq_sum, COUNT(thx) AS thx_count 
FROM dsd
  INNER JOIN 
    (
      SELECT COUNT(id) AS d FROM ds
    ) i

Now I want to use WHERE only to column thx like WHERE thx="y" , so that it will count only all values with "y". 现在我想将WHERE仅用于WHERE thx =“y”这样的列thx ,这样它只会计算所有带“y”的值。

But If I just add WHERE at the end of the query it will affect other columns as well which I don't want to. 但是,如果我只是在查询结束时添加WHERE,它将影响其他列,我不想这样做。

How to do this? 这个怎么做?

Then change your COUNT(thx) AS thx_count to below using CASE expression like 然后使用CASE表达式将您的COUNT(thx) AS thx_count为以下

COUNT(CASE WHEN thx = 'y' THEN 1 ELSE 0 END) AS thx_count

(OR) (要么)

SUM(CASE WHEN thx = 'y' THEN 1 ELSE 0 END) AS thx_count

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

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