SQL子查询作为类似搜索的一部分

发布于 2025-01-31 03:28:20 字数 3912 浏览 3 评论 0原文

来自这个问题我们学会了使用子查询来查找一旦重组的信息。

子查询我们学会了

 SELECT * FROM papers WHERE writer_id IN ( SELECT id FROM writers WHERE boss_id = 4 );

现在,我需要在该表的列值中搜索一个表,以及在另一个表上通过ID相关的列值。

这是同一张表,但是col值为我们的“搜索”参考包含更多文本...

作者

idnameboss_id
1John John Jonno2
2Bill Bosworth2
3Andy Seaside4
4​​ Hank Little4
5Alex Crisp4

作者具有papers 他们写...

papers

ID标题writer_id
1波士顿1
2芝加哥4
3思科3
4西雅图2
5North5

名称

我可以用它来搜索writers ... 仅搜索的 writers.name :(不是我想做的)

SELECT * FROM writers WHERE LOWER(name) LIKE LOWER('%is%');

上述搜索输出 :(不是我想做的)

ID名称BOSS_ID
5ALEX CRISP4

我想从作者返回cols writers.id - 搭配papers.title

例如,如果我搜索“是”,我将两者都得到:

  • Alex Crisp (因为'是'in's'Crisp')
  • Andy Seaside paper带有'is'is'标题'cisco')

输出“ is” search

idtitlewriter_id
2芝加哥4
4SETTLETER2

这是我所拥有的,而不是工作:

SELECT * FROM papers WHERE LOWER(title) LIKE LOWER('%is%') OR writer_id ( writers=writer_id WHERE LOWER(name) LIKE LOWER('%$is%') );

From this Question we learned to use a subquery to find information once-removed.

Subquery we learned :

 SELECT * FROM papers WHERE writer_id IN ( SELECT id FROM writers WHERE boss_id = 4 );

Now, I need to search a table, both in column values that table, and in column values related by id on another table.

Here are the same tables, but col values contain more text for our "searching" reference...

writers :

idnameboss_id
1John Jonno2
2Bill Bosworth2
3Andy Seaside4
4Hank Little4
5Alex Crisp4

The writers have papers they write...

papers :

idtitlewriter_id
1Boston1
2Chicago4
3Cisco3
4Seattle2
5North5

I can use this to search only the names on writers...

Search only writers.name : (Not what I want to do)

SELECT * FROM writers WHERE LOWER(name) LIKE LOWER('%is%');

Output for above search : (Not what I want to do)

idnameboss_id
5Alex Crisp4

I want to return cols from writers (not papers), but searching text both in writers.name and the writers.id-associated papers.title.

For example, if I searched "is", I would get both:

  • Alex Crisp (for 'is' in the name 'Crisp')
  • Andy Seaside (because Andy wrote a paper with 'is' in the title 'Cisco')

Output for "is" search :

idtitlewriter_id
2Chicago4
4Seattle2

Here's what I have that doesn't work:

SELECT * FROM papers WHERE LOWER(title) LIKE LOWER('%is%') OR writer_id ( writers=writer_id WHERE LOWER(name) LIKE LOWER('%$is%') );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月静好 2025-02-07 03:28:20

表达此条件的最佳方法是使用与存在的相关查询

select *
from writers w
where Lower(w.name) like '%is%'
  or exists (
    select * from papers p
    where p.writer_id = w.id and Lower(p.title) like '%is%'
  );

请注意,您不需要在您提供的字符串上使用lower使用较低如果您的整理真正对情况敏感,因为使用该函数使搜索谓词难以理解。

The best way to express this criteria is by using a correlated query with exists:

select *
from writers w
where Lower(w.name) like '%is%'
  or exists (
    select * from papers p
    where p.writer_id = w.id and Lower(p.title) like '%is%'
  );

Note you don't need to use lower on the string you are providing, and you should only use lower if your collation truly is case-sensitive as using the function makes the search predicate unsargable.

蒗幽 2025-02-07 03:28:20

由于您希望从作家返回COLS(不是论文)应该先选择它们,并在标准中使用论文中的内容,

select *
from writers w
where 
   w.name like '%is%'
   or 
   w.id in (select p.writer_id
            paper p
            where p.title like '%is%'
           )

您可以添加较低的功能(我的SQL环境不敏感,所以我不需要它们)

Since you want to return cols from writers (not papers) you should select them first, and use stuff from papers in the criteria

select *
from writers w
where 
   w.name like '%is%'
   or 
   w.id in (select p.writer_id
            paper p
            where p.title like '%is%'
           )

You can add your LOWER functions (my sql environment is not case-sensitive, so I didn't need them)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文