简体   繁体   English

SQL - 如何识别与表的该列中的正常行不匹配的表行

[英]SQL - How to identify table rows that don't match what's normally in that column of the table

I'm having a difficult time trying to figure out how to devise a query that will locate a row in a table that doesn't contain values that usually show up for that column.我很难弄清楚如何 devise 查询将在表中找到不包含通常显示在该列的值的行。

For example, let's say I have the following table:例如,假设我有下表:

在此处输入图像描述

Let's say I have 2000 unique Box Numbers, that have handful of numbered balls in them with what color they are.假设我有 2000 个独特的盒子编号,里面有一把编号的球,它们是什么颜色。 For the most part, all boxes should only have one color type in them (let's say there are a total of 256 colors), but there are some that have more than one color in them.在大多数情况下,所有的盒子都应该只有一种颜色类型(假设总共有 256 种颜色),但有些盒子里的颜色不止一种。

What I'm trying to achieve here is to develop a query that will find which boxes have more than one color inside it.我在这里想要实现的是开发一个查询,该查询将找到哪些盒子里面有不止一种颜色。

develop a query that will find which boxes have more than one color inside it开发一个查询,找出哪些盒子里面有不止一种颜色

If you want entire rows, you can use exists :如果你想要整行,你可以使用exists

select t.*
from mytable t
where exists (
    select 1 from mytable t1 where t1.box_number = t.box_number and t1.color <> t.color
)

If you just want the box numbers, then I would prefer group by and having :如果您只想要框号,那么我更喜欢group byhaving

select box_number
from mytable t
group by box_number
having min(color) <> max(color)

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

相关问题 如何选择与SQL中的另一个表不匹配的表中的行? - How do I select the rows of a table that don't match another table in SQL? 如何显示与另一个表中的行不匹配的行? - How to display rows which don't match a row in another table? 查询表A中与表B中的行不匹配的行 - query for rows in table A that don't match rows in table B 删除表中与另一个表中的行不匹配的行 - Delete rows in a table that don't match rows in another table SQL/Presto:如果值与另一个表的匹配,如何选择行 - SQL/Presto: how to choose rows if the values match with another table's 获取与另一个表中的行不匹配的行 - Get rows that don't match rows in another table 优化 SQL 查询以检查一个大表中的哪些行在另一个更大的表中不存在 - Optimal SQL query to check what rows in one large table don't exist in another even larger table 表上自联接的 SQL 查询并列出与条件不匹配的行 - SQL query for self join on a table and list rows that don't match the condition SQL返回1个表中的所有ID,仅返回另一个中不匹配的ID - SQL return all id's from 1 table and only ones that don't match from another 如何比较同一个表中的两个字段以查看它们是否匹配,不匹配,或 SQL 中的 NULL 与 Group By? - How to compare two fields in the same table to see if they match, don't match, or both NULL in SQL with Group By?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM