2

我有这些表:

Works
 ____________________________________________________________________
|                                                                   |
|id  user_id  cat_id  branch_id  country  budget  explanation       |
|___________________________________________________________________|

Categories
 ____________________________________________________________________
|                                                                   |
|id  category                                                       |
|___________________________________________________________________|

Categories_Branch
 ____________________________________________________________________
|                                                                   |
|id  cat_id  branch                                                 |
|___________________________________________________________________|


I want them displayed like this:
 ____________________________________________________________________
|                                                                   |
|id  country  budget  explanation  category  branch                 |
|___________________________________________________________________|

我不能让它工作,当前的 sql 是:

SELECT works.id, works.country, works.budget, works.explanation, categories.category, categories_branch.branch
FROM works, categories, categories_branch
WHERE works.user_id = ".$_SESSION['customer_id']."
AND works.cat_id = categories.id
AND categories_branch.cat_id = categories.id
GROUP BY works.id

这会使分支组错误,显示错误的组。

有谁知道如何解决这个问题?非常感谢所有帮助。

编辑

这是一张显示我希望它如何排序的图像,问题是显示了错误的分支。

http://i.imgur.com/M7GIwlk.png

4

1 回答 1

1

让我们从这里开始,然后从那里开始:

SELECT works.id, works.country, works.budget, works.explanation, categories.category, categories_branch.branch
FROM works
INNER JOIN categories ON works.cat_id = categories.id
INNER JOIN categories_branch ON categories_branch.cat_id = categories.id
WHERE works.user_id = ".$_SESSION['customer_id']."
于 2013-04-11T15:57:47.243 回答