Php 从数据库中选择多个类别

Php 从数据库中选择多个类别,php,mysql,wordpress,blogs,categories,Php,Mysql,Wordpress,Blogs,Categories,我正在开发一个博客系统,其中博客被分类,我们可以选择我们想要的类别。为此,我必须将表blogs和类别分开。我知道如何从所有类别和单个类别中获取博客,但我不知道如何从多个类别(但不是所有类别)获取博客 我的代码如下所示: <?php $query = ("SELECT blogs_id, title, body, posted_by, category FROM blogs INNER JOIN categories ON categories.category_id=blogs.c

我正在开发一个博客系统,其中博客被分类,我们可以选择我们想要的类别。为此,我必须将表
blogs
类别
分开。我知道如何从所有类别和单个类别中获取博客,但我不知道如何从多个类别(但不是所有类别)获取博客

我的代码如下所示:

 <?php
   $query = ("SELECT blogs_id, title, body, posted_by, category FROM blogs INNER JOIN categories ON categories.category_id=blogs.category_id where category='cat1' ORDER BY blogs_id desc LIMIT 10");
   $result = mysql_query($query) or die("error:".mysql_error());
    while ($row = mysql_fetch_assoc($result)) {
        $title = $row['title'];
        $body = $row['body']; 
        $posted_by = $row['posted_by'];
      ?>
使用以下子句:

或者,您可以使用:


尝试用
代替
(在where条件下)。试试这个:

$query = ("SELECT blogs_id, title, body, posted_by, category FROM blogs INNER JOIN categories ON categories.category_id=blogs.category_id where category='cat1' OR category='cat2' OR category='cat3' ORDER BY blogs_id desc LIMIT 10");
试试这个

$query = ("SELECT blogs_id, title, body, posted_by, category FROM blogs INNER JOIN categories ON categories.category_id=blogs.category_id where category IN ('cat1', 'cat2', 'cat3') ORDER BY blogs_id desc LIMIT 10");
WHERE category = 'cat1'
   OR category = 'cat2'
   OR category = 'cat3'
$query = ("SELECT blogs_id, title, body, posted_by, category FROM blogs INNER JOIN categories ON categories.category_id=blogs.category_id where category='cat1' OR category='cat2' OR category='cat3' ORDER BY blogs_id desc LIMIT 10");
$query = ("SELECT blogs_id, title, body, posted_by, category FROM blogs INNER JOIN categories ON categories.category_id=blogs.category_id where category IN ('cat1', 'cat2', 'cat3') ORDER BY blogs_id desc LIMIT 10");