I am working on a blog system where blogs are categorized and we can choose the category we want to. For this, I have to separate the tables blogs
and categories
. I know how to get blogs from all categories and from a single category, but I don’t know how to get blogs from multiple but not all categories.
My code looks like this:
<?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'];
?>
This code is for selecting a single category and it works well, but now I want to choose multiple (but not all) categories. I tried a few different options, but failed:
<?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' AND category='cat2' AND category='cat3' ORDER BY blogs_id desc LIMIT 10");
This didn’t work.
Use the
IN
clause:Alternatively, you can use
OR
:Try
OR
in place ofAND
(in where condition). try this:Try this