dongzipu7517 2010-04-07 19:00
浏览 44
已采纳

如何编写此SQL语句以获取广告和发布? (PHP / MySQL的)

I am a little confused on the logic of how to write this SQL statement. When a user clicks on a tag, say HTML, it would display all the posts with HTML as its tag. (a post can have multiple tags)

I have three tables:

  1. Posting-->posting_id, title, detail, etc
  2. tags-->tagID, tagname
  3. postingtag-->posting_id, tagID

I want to display all the title of the post and the date added.

    global $dbc;
    $tagID=$_GET['tagID']; //the GET is set by URL

    //part I need help with. I need another WHERE statment to get to the posting table
    $query = "SELECT p.title,p.date_added, t.tagname FROM posting as p, 
    postingtag as pt, tags as t WHERE t.tagID=$tagID";

   $data = mysqli_query($dbc, $query);

  echo '<table>';
  echo '<tr><td><b>Title</b></td><td><b>Date Posted</b></td></tr>';
  while ($row = mysqli_fetch_array($data)) {         
     echo '<tr><td>'.$row['title'].'</td>';
     echo '<td>'.$row['date_added'].'</td></tr>';
     }
  echo '</table>';
}

I am fairly new to mySQL so still trying to figure out the logic of it all :)

  • 写回答

2条回答 默认 最新

  • douya8978 2010-04-07 19:02
    关注

    This is more clearly written using ANSI syntax:

    select p.title, p.date_added, t.tagname 
    from posting p
    inner join postingtag pt on p.postingID = pt.postingID
    inner join tags t on pt.tagID = t.tagID
    where t.tagID=$tagID
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料