当我运行此查询时,为什么其他参与者会获得特定于其他参与者的值?

时间:2019-11-23 02:44:05

标签: mysql sql mysql-workbench

DROP DATABASE IF EXISTS ACTOR;
CREATE DATABASE ACTOR;
USE ACTOR;

CREATE TABLE IF NOT EXISTS actor(
    actorID INT PRIMARY KEY,
    birth_name VARCHAR(50) NOT NULL,
    age INT,
    reside VARCHAR(45) NOT NULL,
    partner VARCHAR(45)
    );

CREATE TABLE IF NOT EXISTS movie(
    movieID INT PRIMARY KEY,
    movie_name VARCHAR(50) NOT NULL,
    year_made INT
);

CREATE TABLE IF NOT EXISTS tag(
    tagID INT PRIMARY KEY,
    tag_list VARCHAR(50) NOT NULL,
    actorID INT REFERENCES actor(actorID)
);

CREATE TABLE IF NOT EXISTS rating(
    ratingID INT PRIMARY KEY,
    actorID INT REFERENCES actor(actorID),
    movieID INT REFERENCES movie(movieID),
    actor_rating DECIMAL(3,1) NOT NULL,
    movie_rating DECIMAL(3,1) NOT NULL
);


INSERT INTO actor VALUES
    (1,'Benjamin Géza Affleck-Boldt',47,'US','Kelleth Cuthbert'),
    (2,'Caleb Casey Affleck',44,'US','Floriana Lima'),
    (3,'Kevin Norwood Bacon',61,'US','Kyra Sedgwick'),
    (4,'Alexander Rae Baldwin III',61,'US','Hilaria Baldwin'),
    (5,'Jason Kent Bateman',50,'US','Amanda Anka');

INSERT INTO movie VALUES
    (11,'Armageddon',1998),
    (12,'American Pie',1999),
    (13,'Apollo 13',1995),
    (14,'The Departed',2006),
    (15,'Hancock',2008)
;

INSERT INTO tag VALUES
    (21,'actor, american, director',1),
    (22,'actor, american, talented',2),
    (23,'actor, american, musician',3),
    (24,'actor, american, funny',4),
    (25,'actor, american, Cute',5)
;

INSERT INTO rating VALUES
    (51,1,11,6.2, 6.2),
    (52,2,12,6.7, 6.1),
    (53,3,13,7.1, 7.0),
    (54,4,14,6.6, 8.0),
    (55,5,15,7.2, 5.9)
    ;


SELECT birth_name, actor_rating
FROM actor, rating
WHERE age IN(61)
;

我正在尝试返回出生年龄和演员年龄(61岁)的评分

查询结果

Kevin Norwood Bacon     6.2
Alexander Rae Baldwin   6.2
Kevin Norwood Bacon     6.7
Alexander Rae Baldwin   6.7
Kevin Norwood Bacon     7.1
Alexander Rae Baldwin   7.1
Kevin Norwood Bacon     6.6
Alexander Rae Baldwin   6.6
Kevin Norwood Bacon     7.2
Alexander Rae Baldwin   7.2

我如何解决这个问题,以免每个演员都获得与其他演员相关的价值? 我已经看过我的屏幕,试图通过这样一个简单的查询来剖析问题,但我仍然不知道是什么原因造成的。可能是我的代码错误,还是我的查询?我正在上初学者课,只是从简单的查询开始,所以请坚持这一点,谢谢。

2 个答案:

答案 0 :(得分:0)

尝试使用此查询:

SELECT birth_name, actor_rating
FROM actor, rating
WHERE age IN(61) and rating.actorID = actor.actorID

答案 1 :(得分:0)

您可以尝试使用加入

SELECT birth_name, actor_rating 
FROM actor 
INNER JOIN rating 
ON actor.actorID=rating.actorID 
WHERE age IN(61)

结果应该这样

enter image description here