在 MySQL 中一次查询两个表

Har*_*rri 3 mysql sql

我有两张桌子;productstorymapstoriesproductstorymapproductIdstoryId列。storiesidstatus列。

我需要查询属于某个产品并具有某种状态的所有故事。我得到的最接近的东西是

SELECT map.*, story.*
FROM productstorymap map, stories story
WHERE map.productId=1 AND story.status=1
Run Code Online (Sandbox Code Playgroud)

但它返回不属于产品 1 的故事。它返回如下内容:

Array
(
    [0] => Array
        (
            [id] => 1
            [productId] => 1
            [storyId] => 1
            [name] => Create a User Story
            [content] => Admin should be able to create user stories.
            [duration] => 1
            [priority] => 0
            [created] => 2010-09-22 17:36:21
            [edited] => 
            [status] => 1
        )

    [1] => Array
        (
            [id] => 4
            [productId] => 1
            [storyId] => 1
            [name] => Bar
            [content] => Xyzzy!
            [duration] => 
            [priority] => 1
            [created] => 2011-02-10 17:50:56
            [edited] => 2011-02-10 17:50:56
            [status] => 1
        )

)
Run Code Online (Sandbox Code Playgroud)

即使 productstorymap 只有两行:

ID  prodcutId   storyID
1   1           1
2   7           4
Run Code Online (Sandbox Code Playgroud)

如果相关,我正在使用 MySQL 和 PHP。

Fru*_*ner 5

我想你忘记在共同领域加入他们,我认为productstorymap.storyIDstories.id

SELECT map.*, story.*
FROM productstorymap map, stories story
WHERE map.productId=1
     AND story.status=1
     and map.storyID = story.id
Run Code Online (Sandbox Code Playgroud)

Join-syntax 也可以是这样的:

SELECT map.*, story.*
FROM productstorymap map
JOIN stories story on map.storyID = story.id
WHERE map.productId=1
     AND story.status=1;
Run Code Online (Sandbox Code Playgroud)