简体   繁体   中英

How can I use joins to fetch mysql rows from one table based on rows that are found in another?

Sorry, I know the question title is really vague, but this might take a little to explain...

I am developing a custom forum with PHP and MySQL... I have tables for categories, boards, and threads. I also have a table for forum permissions. Rows are inserted into the forum permissions table based on what user group can do certain things in each forum. Permissions are read, reply, create, and moderate... just by example. So the columns in the table are like this:

board_id | group_id | read | reply | create | moderate ...

What I want to do is get a list of the 10 most recent threads from any board, but only from boards that the user can read(based on what group they are in and what boards that group can read). I hope I am being clear enough :)

I have the following query that gets boards that the user can read, so maybe that can help

$query_boards = "
            SELECT *
            FROM forum_boards
            WHERE board_id IN (
                    SELECT board_id
                    FROM forum_permissions
                    WHERE group_id = ".$this->user['group']['id']."
                        AND `read` = 1
                    )
                AND category_id = ".$category_id."
                AND parent_id = 0
            ORDER BY position";

The SQL:

CREATE TABLE IF NOT EXISTS `forum_boards` (
  `board_id` int(15) NOT NULL AUTO_INCREMENT,
  `parent_id` int(15) NOT NULL,
  `category_id` int(15) NOT NULL,
  `last_msg_id` int(15) NOT NULL,
  `position` tinyint(1) NOT NULL,
  `title` text NOT NULL,
  `description` text NOT NULL,
  `status` tinyint(1) NOT NULL,
  `redirect_url` text NOT NULL,
  PRIMARY KEY (`board_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `forum_permissions` (
  `permission_id` int(15) NOT NULL AUTO_INCREMENT,
  `board_id` int(15) NOT NULL,
  `group_id` int(15) NOT NULL,
  `read` tinyint(1) NOT NULL,
  `start` tinyint(1) NOT NULL,
  `reply` tinyint(1) NOT NULL,
  `moderate` tinyint(1) NOT NULL,
  PRIMARY KEY (`permission_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `forum_threads` (
  `thread_id` int(15) NOT NULL AUTO_INCREMENT,
  `board_id` int(15) NOT NULL,
  `author_id` int(15) NOT NULL,
  `updater_id` int(15) NOT NULL,
  `first_msg_id` int(15) NOT NULL,
  `last_msg_id` int(15) NOT NULL,
  `title` text NOT NULL,
  `date_posted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `date_updated` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `views` int(15) NOT NULL,
  `status` tinyint(1) NOT NULL,
  `type` tinyint(1) NOT NULL COMMENT '0 normal, 1 sticky, 2 global.',
  PRIMARY KEY (`thread_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

表格:forum_permissions表格:forum_boards表:forum_threads

Try this

"SELECT *
FROM forum_boards AS FB
JOIN forum_permissions FP ON FB.board_id=FP.board_id
WHERE FP.group_id=".$this->USER['group']['id'].
  "AND FP.read=1
  AND FB.category_id =". $category_id.
  "AND FB.parent_id = 0
ORDER BY FB.position";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM