MySQL Forums
Forum List  »  Newbie

Re: working on my new blog and...
Posted by: Claude Martin
Date: January 25, 2005 05:02PM

i didn't like your long names so i did something like this: using unsigned integers would be better - i forgot about that.

CREATE TABLE `user` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 20 ) NOT NULL ,
`pass` VARCHAR( 30 ) NOT NULL ,
PRIMARY KEY ( `id` )
);
CREATE TABLE `blog` (
`id` INT NOT NULL AUTO_INCREMENT ,
`title` VARCHAR( 100 ) NOT NULL ,
`hidden` TINYINT NOT NULL ,
PRIMARY KEY ( `id` )
);
CREATE TABLE `post` (
`id` INT NOT NULL AUTO_INCREMENT ,
`blog` INT NOT NULL ,
`user` INT NOT NULL ,
`time` DATETIME NOT NULL ,
`post` TEXT NOT NULL ,
`hidden` TINYINT NOT NULL ,
PRIMARY KEY ( `id` )
);
CREATE TABLE `auth` (
`id` INT NOT NULL AUTO_INCREMENT ,
`blog` INT NOT NULL ,
`user` INT NOT NULL ,
`permissons` ENUM( 'read', 'write', 'admin' ) NOT NULL ,
PRIMARY KEY ( `id` )
);

SELECT *
FROM post LEFT JOIN blog ON (post.blog = blog.id) LEFT JOIN user ON (user.id = post.user) LEFT JOIN auth ON (auth.blog = post.blog AND auth.user = post.user) WHERE post.blog = 123 AND blog.hidden = 0 AND post.hidden = 0
-> returns all posts from blog 123 that are not hidden, with all infos about the author of the post and such.

SELECT post.*, blog.title, author.name AS author, IF(author.name=user.name,'author',auth.permissons) AS permissons
FROM
post
LEFT JOIN blog ON (post.blog = blog.id)
LEFT JOIN user AS author ON (author.id = post.user)
LEFT JOIN user ON (user.id = 321)
LEFT JOIN auth ON (auth.blog = post.blog AND auth.user = 321)
WHERE post.blog = 123 AND blog.hidden = 0 AND post.hidden = 0
-> returns all info about the post, the name of the blog, the name of the author (writer) of the post, the name of the current user (here it'd be 321). the variable "permissions" is "author" if the user wrote that post or else the permissions set in the auth table or NULL if there is no entry in the auth table.

hope that helps.

http://animalliberation.tk http://veganismus.ch
http://maqi.de http://tierrechtskochbuch.de

Options: ReplyQuote


Subject
Written By
Posted
January 24, 2005 03:52PM
January 25, 2005 04:18PM
January 25, 2005 04:39PM
Re: working on my new blog and...
January 25, 2005 05:02PM
January 28, 2005 01:49PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.