SQL子查询作为类似搜索的一部分
来自这个问题我们学会了使用子查询来查找一旦重组的信息。
子查询我们学会了:
SELECT * FROM papers WHERE writer_id IN ( SELECT id FROM writers WHERE boss_id = 4 );
现在,我需要在该表的列值中搜索一个表,以及在另一个表上通过ID
相关的列值。
这是同一张表,但是col
值为我们的“搜索”参考包含更多文本...
作者
:
id | name | boss_id |
---|---|---|
1 | John John Jonno | 2 |
2 | Bill Bosworth | 2 |
3 | Andy Seaside | 4 |
4 | Hank Little | 4 |
5 | Alex Crisp | 4 |
作者
具有papers 他们写...
papers
:
ID | 标题 | writer_id |
---|---|---|
1 | 波士顿 | 1 |
2 | 芝加哥 | 4 |
3 | 思科 | 3 |
4 | 西雅图 | 2 |
5 | North | 5 |
名称
我可以用它来搜索writers
... 仅搜索的 writers.name
:(不是我想做的)
SELECT * FROM writers WHERE LOWER(name) LIKE LOWER('%is%');
上述搜索输出 :(不是我想做的)
ID | 名称 | BOSS_ID |
---|---|---|
5 | ALEX CRISP | 4 |
我想从作者返回
cols
writers.id
- 搭配papers.title
。
例如,如果我搜索“是”,我将两者都得到:
- Alex Crisp (因为'是'in's'Crisp')
- Andy Seaside
paper
带有'is'is'标题'cisco')
输出“ is” search :
id | title | writer_id |
---|---|---|
2 | 芝加哥 | 4 |
4 | SETTLETER | 2 |
这是我所拥有的,而不是工作:
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
:
id | name | boss_id |
---|---|---|
1 | John Jonno | 2 |
2 | Bill Bosworth | 2 |
3 | Andy Seaside | 4 |
4 | Hank Little | 4 |
5 | Alex Crisp | 4 |
The writers
have papers
they write...
papers
:
id | title | writer_id |
---|---|---|
1 | Boston | 1 |
2 | Chicago | 4 |
3 | Cisco | 3 |
4 | Seattle | 2 |
5 | North | 5 |
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)
id | name | boss_id |
---|---|---|
5 | Alex Crisp | 4 |
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 :
id | title | writer_id |
---|---|---|
2 | Chicago | 4 |
4 | Seattle | 2 |
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
表达此条件的最佳方法是使用与
存在的相关查询
:请注意,您不需要在您提供的字符串上使用
lower
使用较低
如果您的整理真正对情况敏感,因为使用该函数使搜索谓词难以理解。The best way to express this criteria is by using a correlated query with
exists
:Note you don't need to use
lower
on the string you are providing, and you should only uselower
if your collation truly is case-sensitive as using the function makes the search predicate unsargable.由于您希望
从作家返回COLS(不是论文)
应该先选择它们,并在标准中使用论文中的内容,您可以添加较低的功能(我的SQL环境不敏感,所以我不需要它们)
Since you want
to return cols from writers (not papers)
you should select them first, and use stuff from papers in the criteriaYou can add your LOWER functions (my sql environment is not case-sensitive, so I didn't need them)