我有两张 table ; productstorymapstoriesproductstorymapproductIdstoryId 列。 storiesidstatus 列。

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

SELECT map.*, story.*
FROM productstorymap map, stories story
WHERE map.productId=1 AND story.status=1

但它返回不属于产品 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
        )

)

即使 productstorymap 只有两行:
ID  prodcutId   storyID
1   1           1
2   7           4

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

最佳答案

我想你忘了在公共(public)领域加入他们,我认为是 productstorymap.storyIDstories.id

SELECT map.*, story.*
FROM productstorymap map, stories story
WHERE map.productId=1
     AND story.status=1
     and map.storyID = story.id
Join -syntax 也可以如下所示:
SELECT map.*, story.*
FROM productstorymap map
JOIN stories story on map.storyID = story.id
WHERE map.productId=1
     AND story.status=1;

关于sql - 在 MySQL 中一次查询两个表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4960295/

10-11 01:22