2

this is my scheme:

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

assume the username is "foo". i am trying to achieve the following but have no clue how to do it. i am interested in natural join of

userBoard.username = "foo" AND userBoard.boardName = board.name AND board.catId = cat.id AND msg.username = "foo" AND msg.boardName = board.name

what shall be the query for MySQL?

UPDATE: i forgot to mention that i am interested in the following returned values

`board.name, cat.catName, msg.title, msg.text`
0

2 Answers 2

1

Try this:

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"

You will need to choose the type of join based on what do you want to select from your tables.

Sign up to request clarification or add additional context in comments.

4 Comments

this is problematic, it can create a huge table. i wish to visit the table in the order of my logical condition. i.e., firstly retrieve all the userBoard's, then combine it with the catId, etc..
@MrRoth, this is the way your logical condition is written, and it won't create a huge table, it will give you all the data that conform to your logical condition and why it is problematic just try it,
it should not create the table where one of the condition breaks. here it firstly creates the table, then checks the conditions. and i think that you've forgot on on the 4th row
@MrRoth, What do you mean by, create table, it won't create a table then filter it, the selection is the last thing to perform.
0
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 m on b.name = m.boardName
where ub.username = "foo" and m.username = "foo"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.