简体   繁体   English

使用以pk分组并具有count(distinct column_name)= 2的column_name IN('one','two')的查询返回空

[英]query using column_name IN ('one','two') grouped by pk and having count (distinct column_name) = 2 returns empty

Using IN to select a dynamic list of software names that a computer must have to be matched, and the data exists in the database for a known value, the result when using: 使用IN选择计算机必须匹配的软件名称的动态列表,并且数据库中存在一个已知值的数据,使用时的结果:

GROUP BY id
HAVING COUNT(DISTINCT column_name) = count_of_list_values;

returns an empty result set when it should be returning the value expected. 当它应该返回期望值时,将返回一个空结果集。 NOTE it works when you are searching for a single list item, this issue is when that list expands to more than one. 注意它在您搜索单个列表项时有效,而该问题在于该列表扩展到多个列表项时。

Plenty of Google searches, resulting in nothing useful... other than that using phpmyadmin, i have been trying to figure out why it is failing. 大量的Google搜索,除了使用phpmyadmin之外,没有其他有用的结果,我一直在试图弄清为什么它失败了。

Isolated the area where it seems to choking and trying different things with the query to try to get it to work. 隔离看起来似乎令人窒息的区域,并尝试使用查询进行其他操作以使其正常工作。

this is the section of the query that i am using. 这是我正在使用的查询部分。

SELECT
        am_software_archive.asset_name
    FROM
        am_software_archive
    WHERE
        LOWER(am_software_archive.sw_name) IN('nodejs', 'visio 2013')
    GROUP BY
        am_software_archive.id
    HAVING
        COUNT(
            DISTINCT am_software_archive.sw_name
        ) = 2

The data in this table exists and is valid so it should work... The table definition 该表中的数据存在并且有效,因此应该可以使用...表定义

CREATE TABLE IF NOT EXISTS am_software_archive(
                    id BIGINT NOT NULL UNIQUE,
                    asset_name VARCHAR(10) NOT NULL,
                    sw_name VARCHAR(150) NOT NULL,
                    sw_developer BIGINT NOT NULL,
                    sw_key VARCHAR(50) DEFAULT NULL,
                    sw_osver VARCHAR(15) DEFAULT NULL,
                    CONSTRAINT PK_software PRIMARY KEY(id, asset_name),
                    INDEX idx_sw_name_asset(asset_name,sw_name),
                    INDEX idx_sw_key_asset_name(asset_name,sw_key),
                    INDEX idx_sw_name_sw_key(sw_name,sw_key),
                    INDEX idx_osver_sw_name(sw_name,sw_osver),
                    INDEX idx_osver_asset_name(asset_name,sw_osver)
                )

In my database the expected result would be: "ABX50269" 在我的数据库中,预期结果将是:“ ABX50269”

Actual results well are empty, but shouldn't be. 实际结果是空的,但不应该这样。

You group by the id , which is unique so there cannot ever be two or more (different) sw_name per id . 您按id分组,该id是唯一的,因此每个id不能有两个或多个(不同的) sw_name

As asset_name is the column in the list for SELECT , I think you actually want to group by asset_name . 由于asset_nameSELECT列表中的列,我认为您实际上是想按asset_name

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

相关问题 为什么 HAVING 可以引用 COUNT(column_name) 而不能引用 column_name 本身? - Why can HAVING reference COUNT(column_name) but not the column_name itself? 使用MySQL在column_name之后移动column_name - Move column_name AFTER Column_name using MySQL MySQL:在列列表中以及在HAVING子句中再次使用COUNT(column_name)。 这会导致COUNT(column_name)操作运行两次吗? - MySQL: Using COUNT(column_name) in the column list, and again in the HAVING clause. Does this cause the COUNT(column_name) operation to run twice? 为什么count(column_name)的行为如此? - Why is count(column_name) behaving like this? mysql where count(column_name)= 1? - mysql where count(column_name) = 1? MySQL:在“WHERE”中使用字符串<column_name>在<values> “ 询问</values></column_name> - MySQL: Using a string in a "WHERE <column_name> IN <values>" Query 为什么column_name =(选择column_name)起作用,而column_name = null不起作用 - Why does column_name = (select column_name) work and not column_name=null count(*)和count(column_name),差异是什么? - count(*) and count(column_name), what's the diff? 添加 column_name != 3 返回的结果要少得多 - Adding column_name != 3 returns much fewer results MySql 如何 COUNT(column_name = &#39;foobar&#39; or null) 工作? - MySql how COUNT(column_name = 'foobar' or null) works?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM