简体   繁体   English

从存在ID(从另一表)的一个表中选择

[英]Select from one table where id (from another table) exists

First of all, I'd just want to say that I'm sorry for the poor title. 首先,我只想对这个可怜的头衔感到抱歉。 I'm really struggling with explaining the problem I'm facing in just a short sentence. 我真的很难用一句话来解释我面临的问题。

I have a table called actors which contains an aID (primary key) and an aName and some more stuff. 我有一个称为actors的表,其中包含一个aID(主键)和一个aName以及其他内容。 I also have a table called videos which contains vID (primary key) and other data. 我还有一个称为视频的表,其中包含vID(主键)和其他数据。 The third and last table I have is called connections and that one contains a primary key, a cVideoID and a cActorID . 我拥有的第三个也是最后一个表称为连接 ,该表包含一个主键,一个cVideoID和一个cActorID

Let's say I have created a video and when I navigate to the video *www.example.com/video.php?v=primary_key* I'd like to print out all actors who are in that movie (actor1, actor2, actor3 and actor4). 假设我创建了一个视频,当我导航至该视频* www.example.com / video.php?v = primary_key *时,我想打印出该电影中的所有演员(actor1,actor2,actor3和演员4)。 Therefor I created the connections -table to keep track of all movie-actor connections. 为此,我创建了连接表-来跟踪所有电影演员的连接。 For every movie I create I connect the actors with the movie. 对于我制作的每部电影,我都会将演员与电影联系起来。

I thought that I could do something like this: 我以为我可以做这样的事情:

<?php
    $result2 = mysql_query("SELECT `actors`.`aID`, `actors`.`aName` FROM `actors` WHERE `connections`.`cVideoID` = {$_get['v']}");
    while($actors = mysql_fetch_array($result2))
    {
        echo "<a href='actor.php?id={$actors['aID']}>{$actors['aName']}</a> ';
    }
?>

But it seems like that's not working. 但这似乎不起作用。 Any ideas? 有任何想法吗?

What you want to do, is select all of the entries from connections , where the video ID is the selected video. 您要做的是从connections选择所有条目,其中视频ID是所选视频。 Then, you JOIN on the actors table, to get all of the information about the actors that were found. 然后,您在actors表上进行JOIN,以获取有关找到的actor的所有信息。

Example: Get all of the actor's names for a specific video ID: 示例:获取特定视频ID的所有演员姓名:

SELECT a.aName 
FROM connections c 
  LEFT JOIN actors a 
  ON a.aID = c.aID 
WHERE c.vID = 1;

SQL Fiddle SQL小提琴

What you need here is a join. 您需要的是加入。 A normal left join works like this: 普通的左联接如下所示:

LEFT JOIN [name of table] [name of table you will want to use] 
       ON ([where statements searching for the right columns to join]) 

So your query will look something like this: 因此,您的查询将如下所示:

   SELECT a.aID, a.aName 
     FROM `connections` c 
LEFT JOIN `author` a ON (c.cActorID=a.aID) 
    WHERE c.cVideoID=[id of the video]

Now first of all you say the database you want to catch the columns aID and aName from the table a next you use the FROM statement to "import" the connections table as "c". 现在,首先您要说要从表中捕获列aID和aName的数据库,然后使用FROM语句将连接表“导入”为“ c”。 You then load the author table and make it accessible as "a" (see the select statement a.[...]) and you also say that it should join the two tables ON every c.cActorID=a.aID and in the end you make a where statement to declare you are only searching for videos with the c.cVideoId=[id] 然后,您加载作者表并将其作为“ a”进行访问(请参见select语句a。[...]),并且您还说它应该在每个c.cActorID = a.aID以及结束时,您需要在where语句中声明您仅在搜索c.cVideoId = [id]的视频

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

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