SQL从具有多个where子句的一张表中进行选择

编程入门 行业动态 更新时间:2024-10-27 18:24:33
本文介绍了SQL从具有多个where子句的一张表中进行选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在高处和低处寻找这个问题的答案,但没有运气.我相信它不会像我想象的那么复杂.

Searching high and low for an answer to this one and having no luck. I'm sure it can't be as complicated as I think it is.

我有一张带有标题和代码的表格.这些代码是唯一的,可以有多个标题代码.我希望能够选择那些包含我列出的代码的标题.

I have one table with titles and codes. The codes are unique and can have more than one titlecode. I want to be able to select those titles that have codes I list.

Titles Codes ----------------------------------- Book1 001 Book2 010 Book2 020 Book2 021 Book3 030 Book3 040

所以我希望能够返回代码为 020 和 021 的标题.或者我列出的任何内容.在这种情况下,它只会返回 Book2,因为该标题具有这两个代码.

So I want to be able to return Titles that have codes 020 and 021. Or whatever I list. In this case it would just return Book2 as that Title has those two codes.

我最初尝试过

SELECT Titles FROM table WHERE Codes = 020 AND Codes = 021

但是返回零结果,我可以理解为什么.没有一行包含多个代码条目

but that returned zero results and I could see why. No row contains more than one Codes entry

SELECT Titles FROM table WHERE Codes = 020 OR Codes = 021

返回两者之一的标题.所以我一直在尝试使用 GROUP BY 和一个子查询来尝试获取它,但没有运气.有人可以帮忙吗?提前致谢

returns Titles that are either. So I've been trying to use GROUP BY and also a subquery to try and get it but having no luck. Can anyone help please? Thanks in advance

推荐答案

为了获得您正在寻找的数据,您需要在您的表上使用自联接:

In order to get the data you're looking for you will need to use a self-join on your table:

SELECT * FROM table t1 JOIN table t2 ON t1.Title = t2.Title WHERE t1.Codes = '020' AND t2.Codes = '021'

更多推荐

SQL从具有多个where子句的一张表中进行选择

本文发布于:2023-10-14 21:33:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1492277.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子句   多个   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!