简体   繁体   English

如何在联接另一个表时搜索表中的所有列?

[英]How to search all columns in a table while joining another table?

mysql> describe Movies;
+-------------+--------------+------+-----+---------+-------+
| Field       | Type         | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| MOVIE_ID    | char(5)      | NO   | PRI |         |       |
| TITLE       | varchar(100) | NO   |     | NULL    |       |
| RATING      | varchar(3)   | YES  |     | NULL    |       |
| GENRE       | varchar(15)  | YES  |     | NULL    |       |
| RELEASEDATE | char(4)      | YES  |     | NULL    |       |
| ACTOR_ID    | char(5)      | NO   | MUL | NULL    |       |
| ACTOR_ID_2  | char(5)      | YES  | MUL | NULL    |       |
| ACTOR_ID_3  | char(5)      | YES  | MUL | NULL    |       |
+-------------+--------------+------+-----+---------+-------+


mysql> describe Actors;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| ACTOR_ID | char(5)     | NO   | PRI |         |       |
| FNAME    | varchar(40) | NO   |     | NULL    |       |
| LNAME    | varchar(40) | NO   |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+

First off, I know I should've made my tables different and that it is not good practice to have actorid, actorid2, actorid3, but this is a group project I'm working on, and I haven't been able to change the table. 首先,我知道我应该使表有所不同,并且具有actorid,actorid2,actorid3并不是一个好习惯,但这是我正在从事的小组项目,但是我无法更改表。 So I'm trying to make it work. 所以我正在努力使它起作用。 In the query: 在查询中:

select * from Movies join Actors on Actors.ACTOR_ID in (Movies.ACTOR_ID, Movies.ACTOR_ID_2, Movies.ACTOR_ID_3);

I am joining all the actors into the movies so I know which actor names are in which movies. 我正在将所有演员加入电影中,所以我知道哪些演员姓名在哪些电影中。 I want to have a search function where the user can search any movie, genre, or actor and get all the information back. 我想拥有一个搜索功能,用户可以搜索任何电影,体裁或演员并获取所有信息。 So I have made this query: 所以我做了这个查询:

select * from Movies where 'Step Brothers' in (select * from Movies join Actors on Actors.ACTOR_ID in (Movies.ACTOR_ID, Movies.ACTOR_ID_2, Movies.ACTOR_ID_3));

Step Brothers is of course a movie title so I want it to pop up will all columns of Movies where Step Brothers exists. 当然,Step Brothers是一个电影标题,因此我希望它弹出,以显示Step Brothers存在的电影的所有列。 Or if they searched for Will Ferrell (He is an actor in Step Brothers) then all the movies Will Ferrell stars in will pop up. 或者,如果他们搜索了Will Ferrell(他是Step Brothers的演员),那么将会弹出Will Ferrell出演的所有电影。 How do I do this? 我该怎么做呢? When I execute 当我执行

 select * from Movies where 'Step Brothers' in (select * from Movies join Actors on Actors.ACTOR_ID in (Movies.ACTOR_ID, Movies.ACTOR_ID_2, Movies.ACTOR_ID_3));

I get an Operand should contain 1 column(s) but I'm selecting all columns from each table so they should be the same amount of columns, right? 我得到一个Operand should contain 1 column(s)但是我要从每个表中选择所有列,所以它们应该是相同数量的列,对吗?

You can try this one: 您可以尝试以下一种方法:

select m.*, a.*
from Movies m
join Actors a on a.ACTOR_ID in (m.ACTOR_ID, m.ACTOR_ID_2, m.ACTOR_ID_3)
where 'your search string' in (
    concat(a.FNAME, ' ', a.LNAME),
    m.TITLE,
    m.GENRE
)

It will show you the rows from the joined result if the search string matches actors name, movie title or movie genre. 如果搜索字符串匹配演员姓名,电影标题或电影流派,它将显示合并结果中的行。

You can not say "search in all columns" - You need to specify the columns you want to search in. 您不能说“在所有列中搜索”-您需要指定要在其中搜索的列。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM