这是我的计划:

board(name, catId)
cat(id, catName)
userBoard(boardName, username)
msg(boardName, username, title, text)

假设用户名是“foo”。我正在努力实现以下目标,但不知道如何做到。我对自然结合感兴趣
userBoard.username = "foo" AND userBoard.boardName = board.name AND board.catId = cat.id AND msg.username = "foo" AND msg.boardName = board.name
对mysql的查询是什么?
更新:
我忘了提到我对以下返回值感兴趣
`board.name, cat.catName, msg.title, msg.text`

最佳答案

试试这个:

Select b.name, c.catName, m.title, m.text
from board b
   inner join Cat c on b.catId = c.id
   inner join userBoard ub on b.name = ub.boardName
   inner join msg on m b.name = m.boardName
where ub.username = "foo" and m.username = "foo"

您需要根据要从表中选择的内容来选择联接类型。

10-06 15:23