简体   繁体   English

SQL查询帮助(连接两个表)

[英]SQL Query Help (Joining two tables)

I have two tables in my database with the following schemas:我的数据库中有两个表,具有以下架构:

ratings table:
usedId
movieId
rating

info table
movieId
imdbId

The movieId values in each table are the same (ie movieId 1 is the same movie in both).每个表中的movieId 值是相同的(即movieId 1 在两个表中是同一部电影)。

Each movieId & userId appears multiple times in the ratings table.每个 movieId 和 userId 在评分表中出现多次。

What I am trying to do is create a new table that looks like:我想要做的是创建一个新表,看起来像:

new table:
userID
movieId
imdbId
rating

Where the imdbId is added for each row that matches the movieId of its original table.为与原始表的 movieId 匹配的每一行添加 imdbId 的位置。

select ratings.userID, ratings.movieId, info.imdbId, ratings.rating from
ratings inner join info on ratings.movieId = info.movieId

Or you can manual join the two tables:或者您可以手动连接两个表:

SELECT r.userID, r.movieId, i.imdbId, r.rating 
FROM ratings r, info i 
WHERE r.movieId = i.movieId

You should use SQL Join to create your expected table.您应该使用SQL Join创建您想要的表。

select Rating.userID, Rating.movieId, Info.imdbId, Rating.rating 
from ratings as Rating
inner join 
info as Info on ratings.movieId = info.movieId

To get more details of join check this inner join link要获取有关加入的更多详细信息,请查看此内部加入链接

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

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