简体   繁体   English

如何选择表中的所有记录(不包括另一个记录)

[英]How to select all records in a table excluding records from another

I have 2 tables that hold information about pages and any categories that the pages belong to, the pages do not have to have any category and can also be in multiple categories. 我有2个表,这些表包含有关页面以及页面所属的任何类别的信息,页面不必具有任何类别,也可以处于多个类别中。

page
 pageid      title
  1          page1
  2          page2
  3          page3
  4          page4

category
 pageid   category
  2       cat1
  2       obsolete
  3       cat2

I want to write a query that will select all pages as long as they are not categorised as 'obsolete'. 我想写一个查询,将选择所有页面,只要它们没有被归类为“过时”即可。

From the above, the output I am looking for is : 从上面,我正在寻找的输出是:

1     page1
3     page3
4     page4

So page 2 is excluded as one of its categories is 'obsolete' all other rows in the page table should be listed only once even if they have no categories or multiple categories in the category table. 因此,将第2页排除在外,因为它的类别之一“已过时”,即使表类别中没有任何类别或多个类别,页面表格中的所有其他行也应仅列出一次。

I have tried multiple combinations of subquery and joins but cannot achieve the output I am looking to produce. 我尝试了子查询和联接的多种组合,但是无法实现我想要产生的输出。

Use NOT EXISTS 使用NOT EXISTS

SELECT *
FROM   page p
WHERE  NOT EXISTS (SELECT pageid
                   FROM   category c
                   WHERE  p.pageid = c.pageid
                          AND category = 'obsolete'); 

Another way using Conditional Aggregate and EXISTS 使用Conditional AggregateEXISTS另一种方法

SELECT *
FROM   page p
WHERE  EXISTS (SELECT 1
               FROM   category c
               WHERE  p.pageid = c.pageid
               HAVING Count(CASE WHEN category = 'obsolete' THEN 1 END) = 0); 
select * from page 
where pageid not in (select pageid from category where category='obsolete');

or 要么

 select distinct title from page 
 where pageid not in (select pageid from category where category='obsolete');

暂无
暂无

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

相关问题 仅当在另一个表中找到所有匹配记录时,如何 select 记录? - How to select records only if all matching records found in another table? 从一个表中获取记录,但不从另一个表中获取记录 - Get records from one table excluding records from another 如何从数据库表中删除记录(不包括来自另一个SQL的记录) - How to delete records from database table excluding records from another SQL 如何 select 一个表中的所有记录在另一个表中不存在于另一个表中的某些条件下? - How to select all records from one table that do not exist in another table for certain condition in another table? Select 基于条件的一个表中的所有记录,而不是另一个表中的所有记录 - Select all records from one table not in another table based on a condition 如何选择不在另一个表中的记录? - How to select records that are not in another table? 根据另一个表中的规则从表中排除记录 - Excluding records from table based on rules from another table 查询问题:是否有更好的方法从一个表中选择所有记录,以及从另一表中选择不匹配的记录? - Query question: Is there a better way to select all records from one table, along with unmatched records from another table? SQL查询-从一个表中选择全部,在另一个表中匹配记录 - SQL Query - select all from one table with matching records in another 如何从一个表中选择与SQL Server中的另一个表完全匹配的所有记录? - How to select all records from one table that exactly match another table in SQL Server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM