简体   繁体   English

Select 具有一个值但没有来自同一个表的另一个值的行

[英]Select rows that has one value but not another from same table

I have a table like below in MySQL.我在 MySQL 中有一张如下表。 I want all the rows that have name A but not name B.我想要所有名称为 A 但不是名称 B 的行。

在此处输入图像描述

I want a result like this.我想要这样的结果。 Thank you for helping.感谢您的帮助。

在此处输入图像描述

I have tried the following query.我尝试了以下查询。 It is not working.它不工作。

Select fk_id from TableA
where  name  not like 'B'
group by fk_id
having  name like 'A'

Your question isn't particularly clear but it sounds like you want fk_id having an associated name value of A and where any record with the same fk_id and name of B does not exist.您的问题不是特别清楚,但听起来您希望fk_id具有关联的nameA并且不存在具有相同fk_idname B的任何记录。

Try this尝试这个

SELECT fk_id FROM TableA a
WHERE a.name = 'A'
AND NOT EXISTS (
  SELECT 1 FROM TableA b
  WHERE b.name = 'B'
  AND a.fk_id = b.fk_id
)

Demo ~ http://sqlfiddle.com/#!9/ba0df2/2演示 ~ http://sqlfiddle.com/#!9/ba0df2/2

WITH b_rows AS(
    SELECT fk_id  AS fk_id_b,
           '1'    AS flg_b
    FROM TableA
    WHERE name  = 'B'
)
SELECT fk_id
FROM TableA
    LEFT JOIN b_rows ON TableA.fk_id = b_rows.fk_id_b
WHERE name  = 'A'
    AND flg_b <> '1' 

How about using GROUP_CONCAT() in HAVING like:HAVING中使用GROUP_CONCAT()怎么样:

SELECT fk_id  
FROM TableA
GROUP BY fk_id
HAVING GROUP_CONCAT(name) LIKE '%A%' 
   AND GROUP_CONCAT(name) NOT LIKE '%B%';

Basically, the result of GROUP_CONCAT(name) with GROUP BY fk_id returns the following:基本上,带有GROUP BY fk_idGROUP_CONCAT(name)的结果返回以下内容:

+--------------------+-------+
| GROUP_CONCAT(name) | fk_id |
+--------------------+-------+
| C                  | l     |
| A,B                | m     |
| A,C                | n     |
+--------------------+-------+

So we can make use of that result to do the filtering in HAVING .所以我们可以利用该结果在HAVING中进行过滤。 This query is similar to:此查询类似于:

SELECT fk_id
  FROM
   (SELECT GROUP_CONCAT(name) AS names, fk_id
    FROM TableA
     GROUP BY fk_id) A
  WHERE names LIKE '%A%'
    AND names NOT LIKE '%B%';

This instead is using a sub-query then doing the filtering afterward using WHERE .相反,这是使用子查询,然后使用WHERE进行过滤。

Demo Fiddle 演示小提琴

暂无
暂无

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

相关问题 从表中选择行,其中具有相同id的另一个表中的行在另一列中具有特定值 - Select rows from a table where row in another table with same id has a particular value in another column mysql从同一表中选择一个值作为另一个值 - mysql select one value as another value from same table 从一个表中选择结果,并为每一行从另一张表中选择具有相同ID的所有行 - Select results from one table and for each row select all rows that have the same id from another table 从一个表中选择随机值并插入到同一数据库中的另一个?? - select randum value from one table and inserted to another in the same database?? 如何选择或区分同一表中的两行,其中一个字段的值相同,而另一字段不同 - how to select or distignuish two rows in the same table with the same value on one field but different on another 从一个表中选择所有行,并为另一表中的每一行选择一个特定值 - Select all rows from one table and one specific value for each row from another table SELECT DISTINCT rows FROM a table HAVING MIN(value) FROM another table - SELECT DISTINCT rows FROM one table HAVING MIN(value) FROM another table 从一个表中选择与另一表中的值匹配的不同行和随机行 - Select distinct and random rows from one table that match a value from another table 如何基于另一个表中相同值的行为从一个表中选择值? - How to SELECT value from one table based on same value's behavior in another table? 在同一行的同一MySQL表上选择一个字段的值到另一个字段? - Select value of one field into another on the same MySQL table in from the same row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM