【问题标题】:getting blogs from database by specific category按特定类别从数据库中获取博客
【发布时间】:2015-09-08 03:58:14
【问题描述】:

我已经创建了一个包含类别的博客系统,我在数据库中有两个表,表 blogs 带有 blog_idtitlebodycategory_id,第二个表带有列 categorycategory_id。要从所有类别中获取博客,我使用此代码并且效果很好。

  $query = ("SELECT blogs_id, title, body, posted_by, category FROM blogs  INNER JOIN categories ON categories.category_id=blogs.category_id  ORDER BY  blogs_id desc LIMIT 10");
 $result = mysql_query($query);
 $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 category=that category”它不起作用所以我尝试了category_id,但它也失败了。我的代码是这样的

        $query = ("SELECT blogs_id, title, body, posted_by, category FROM blogs INNER JOIN categories ON categories.category_id=blogs.category_id where category=anycategory ORDER BY blogs_id desc LIMIT 10");

【问题讨论】:

  • 在 where 子句中使用 '。像这样where category= 'anycategory'
  • 尝试用简单的引号来放置现有的类别。

标签: php mysql blogs categories


【解决方案1】:

where 子句上使用'

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

并且确保有一个表格字段在您的表格中调用category

【讨论】:

  • 别名也不含歧义
  • 当我看到这个时,我想的是,这里可能会出现错误 1052
  • 我也想知道如何从多个类别中进行选择 像这样尝试过 where category='cat1','cat2' 这不起作用然后尝试 where category='cat1,cat2' 但是这个也没有用。 :(
  • @harman 使用 and。像这样where category = 'cat1' and category = 'cat2'
  • @harman no ts 将起作用。 select * from temp where category = 'cat1' and category = 'cat2'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 2022-11-21
  • 2018-06-26
  • 2022-12-20
  • 1970-01-01
相关资源
最近更新 更多