开发者

MySQL multiple tables access query

开发者 https://www.devze.com 2023-01-12 00:09 出处:网络
I am new to web development... mostly i learn my own.. I got to design a database structure so to store items similar to tags and posts in a blog.

I am new to web development... mostly i learn my own..

I got to design a database structure so to store items similar to tags and posts in a blog. The design would be:

_____________
Tag Table
TAGID | TAGNAME
_____________
_____________
Post Table
POSTID | POSTNAME
_____________
_____________
Tag Post Relation Table
TAGID | POSTID
_____________

Now what i have read online over the time, this is the best way to store TAGS and relate it to the posts.. (please correct me if i am wrong) Now my doubt is how to retrieve all TAGNAME(s) associated to a POSTID.

Sorry for such a newb question but even i was unable to figure out what query do i search in google.

And please recommend me some good开发者_如何学Python notes/tutorial on MySQL.


how's this?

Select Tag.tagname
from tag, tagpost
where tagpost.postid = '$something'
    and tagpost.tagid = tag.tagid;

Alternate syntax with JOIN keyword:

 Select Tag.tagname
 from tag
    inner join tagpost on tagid = tagpost.tagid
 where tagpost.postid = `$something`


To get all tags for a specific post you'd execute :

select tags.tagname from tags
inner join tags_posts on tags_posts.tagid=tags.tagid
where tags_posts.postid=:postid

And bind the post ID to :postid.

0

精彩评论

暂无评论...
验证码 换一张
取 消