使用JOIN从另一个表中获取信息

时间:2013-03-25 14:06:49

标签: sql database join

目前,我有这个SQL查询来尝试从collection_actors获取信息:

SELECT ca.id_imdb AS IMDb_ID,
       caat.id_collection AS collection_ID,
       ca.data_name AS actors_name

FROM collection_actors AS ca
JOIN collection_actors_assignedto AS caat
ON ca.id_imdb = caat.id_collection
WHERE caat.id_collection = 'tt1104001'

这是两个表的数据库:

collection_actors
    id
    id_user
    id_imdb
    data_name
    date_birth
    date_added
    date_edited
    information_ipaddress


collection_actors_assignedto
    id
    id_user
    id_actor
    id_collection
    date_added
    information_ipaddress

问题在于,即使有与IMDb ID匹配的数据,我也无法从collection_actors获取任何信息!我怎样才能让它发挥作用?

2 个答案:

答案 0 :(得分:0)

试试这个:

SELECT ca.id_imdb AS IMDb_ID,
       caat.id_collection AS collection_ID,
       ca.data_name AS actors_name

FROM collection_actors AS ca, collection_actors_assignedto AS caat
WHERE (ca.id_imdb = caat.id_collection) AND (caat.id_collection = 'tt1104001')

答案 1 :(得分:0)

使用此sql代码段,这是基于您的代码,但您需要确保id_collection中的值与id_imdb字段中的数据匹配,否则您将永远不会得到任何结果。

SELECT ca.id_imdb AS IMDb_ID
, caat.id_collection AS collection_ID
, ca.data_name AS actors_name
FROM collection_actors AS ca
INNER JOIN collection_actors_assignedto AS caat ON ca.id_imdb = caat.id_collection
WHERE caat.id_collection = 'tt1104001'