从几个主键sql获取行

uet 发布于 2019-03-09 rows 最后更新 2019-03-09 14:31 3 浏览

我有这些表格:

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
已邀请:

rsaepe

赞同来自:

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

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']."