简体   繁体   中英

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. 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. 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. 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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