我有两张桌子; productstorymap
和stories
。 productstorymap
有productId
和storyId
列。 stories
有id
和status
列。
我需要查询属于某个产品且具有特定状态的所有故事。我得到的最近的事情是
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,如果这是相关的。
答案 0 :(得分:4)
我认为您忘记加入公共领域,我假设是productstorymap.storyID
和stories.id
SELECT map.*, story.*
FROM productstorymap map, stories story
WHERE map.productId=1
AND story.status=1
and map.storyID = story.id
Join
- 语法也可以如下所示:
SELECT map.*, story.*
FROM productstorymap map
JOIN stories story on map.storyID = story.id
WHERE map.productId=1
AND story.status=1;
答案 1 :(得分:2)
SELECT *
FROM productstorymap prod
JOIN stories story ON prod.storyId = story.Id
WHERE prod.productId = 1
AND story.status = 1
答案 2 :(得分:0)
SELECT map.*, story.*
FROM productstorymap map, stories story
WHERE map.productId=1 AND story.status=1 AND map.storyId = story.Id
这是在表之间调用JOIN。寻找它