duangu8264 2014-07-27 04:03
浏览 56
已采纳

如何收集两个不同查询的COUNT(*)?

I tried the following query and got an error in MySQL. I kind of know why it's throwing me a syntax error (it's related to the two DISTINCT queries being run on the same line) but still believe that its possible (somehow). The two columns card_type and split_type each contain one of five values ("Attack", "Foundation", "Character", "Asset", "Action"). What I would like to do is create a query that would count a record as a +1 if "Attack" appeared in either card_type or split_type.

SELECT DISTINCT(ufs.card_type), DISTINCT(ufs.split_type), COUNT(*) AS COUNT
    FROM jg1_products p 
    LEFT JOIN jg1_product_types pt ON p.products_type = pt.type_id 
    LEFT JOIN jg1_products_to_categories ptc ON p.products_id = ptc.products_id 
    LEFT JOIN jg1_cards_ufs ufs ON ufs.products_id = p.products_id
WHERE type_handler LIKE "%product_cards%"
    AND ptc.categories_id = 89
GROUP BY ufs.card_type

I guess I should be a BIT clearer in my explanation:

I was hoping that the SQL query would return the total number of results which have met a condition in either Column A or Column B.

EXAMPLE: If the word "Attack" appeared in either column A or column B, count that as one.

If the word "Attack" appeared in Column A but Column B contains the word "Foundation", That would be +1 to Attack and +1 to Foundation.

In the end, the function/SQL would return to number of times that word (either of the five possibles) would appear between those two columns. So in short I used the "DISTINCT" command (incorrect I'll add) so that all the distinct values in column A are returned and the same for B. If they match, count that as one.

  • 写回答

3条回答 默认 最新

  • duancilan5124 2014-07-27 04:46
    关注

    Q: "a query that would count a record as a +1 if "Attack" appeared in either card_type or split_type."

    A: It's not clear what resultset you want to return.

    EDIT

    Based on your comment/clarification, if I'm understanding this correctly, if the rows returned by the SELECT were something like this example:

    card_type  split_type
    ---------  -----------
    Attack     Attack
    Attack     Foundation
    Attack     Crescendo
    Foundation Foundation
    

    You want a resultset something like this:

    Attack     3
    Foundation 2
    Crescendo  1 
    

    You want 3 returned for "Attack", because three rows had the value 'Attack' in either card_type or split_type. That is, you don't want to return a count of 4, the number of times the value appeared.

    To get that result, using a COUNT aggregate, I would run this as two separate queries, and combine the results of the two queries using a UNION ALL set operator. The first query would get a count by just card_type, the second query would get a count by split_type. The "trick" would be for the second query to exclude any rows where the split_type matches the card_type.

    The two combined queries would be used as an inline view, the outer query would combine the separate counts using a SUM() aggregate function.

    I would do the query using a form something like this:

    SELECT c.type
         , SUM(c.cnt) AS cnt
      FROM ( SELECT ufs.card_type AS `type`
                  , COUNT(1) AS cnt 
               FROM ...
    
              GROUP BY ufs.card_type
    
              UNION ALL
    
             SELECT ufs.split_type AS `type`
                  , COUNT(1) AS cnt
               FROM ...
    
                AND NOT (ufs.split_type <=> ufs.card_type)
              GROUP BY ufs.split_type    
           ) c
     GROUP BY c.type
    

    You'd plug in the row source of the original query two times, replacing the ... in the query above.


    Previous answer:

    Assuming that you have a SELECT that returns the rows you want checked, one "trick" is to use an expression in the SELECT list to perform a conditional test, and return either a zero or one, and then use SUM() aggregate to return a "count" of the records that meet the specification..

    SELECT SUM(IF(ufs.card_type LIKE '%Attack%' OR ufs.split_type LIKE '%Attack%',1,0)) AS cnt
      FROM jg1_products p 
      LEFT JOIN jg1_product_types pt ON p.products_type = pt.type_id 
      LEFT JOIN jg1_products_to_categories ptc ON p.products_id = ptc.products_id 
      LEFT JOIN jg1_cards_ufs ufs ON ufs.products_id = p.products_id
     WHERE type_handler LIKE "%product_cards%"
       AND ptc.categories_id = 89
    

    This query returns a single row, unlike your original query that returns multiple rows. (Again, it's not clear what resultset you want returned; if you actually want to return a count for each distinct card_type, which would be returned if we included a GROUP BY ufs.card_type clause.

    MySQL also provides a convenient shorthand for the boolean expression: the evaluation of a boolean expression returns 1 if TRUE, 0 if FALSE, and NULL if NULL. So this expression:

    SELECT SUM(ufs.card_type LIKE '%Attack%' OR ufs.split_type LIKE '%Attack%')
      FROM ...
    

    is equivalent to the expression in the query above, except for the handling of NULL values.

    It's not clear whether you want to check if the column "contains" the string 'Attack' as part of the string, or is the entire string; to check if the value of the column is exactly equal to 'Attack', use the equality comparison instead of LIKE

    SELECT SUM(ufs.card_type = 'Attack' OR ufs.split_type = 'Attack') AS cnt
      FROM ...
    

    NOTE

    DISTINCT is not a function, it's a keyword.

    The valid syntax is SELECT DISTINCT expr1, expr2, ... exprN FROM ....

    It's invalid to include the DISTINCT keyword multiple times after the SELECT keyword, or in a position other than immediately following SELECT. (The DISTINCT keyword can also be included the COUNT() aggregate function, e.g. SELECT COUNT(DISTINCT expr), but that's entirely different than SELECT DISTINCT.

    The parens are entirely ignored. That is, SELECT DISTINCT(foo) is identical to SELECT DISTINCT foo. Including parens is entirely unnecessary, and makes it look like DISTINCT is a function (which it is not.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条