dpwtr666638 2016-05-24 03:55
浏览 13
已采纳

将多个表中的数据转换为单行

I'm trying to retrieve data from 2 tables and combine multiple rows into a single while loop

posts

post_id content          
------  -------      
   1    content1                     
   2    content2           
   3    content3  
   4    content4


comments

id      post_id   content   
------  ------    ------              
   1      1        Wharton university           
   1      2        Yale University         

sql code I write

    mysqli_query( $connect, "SELECT * FROM `posts`
 INNER JOIN  comments ON posts.post_id =  comments.post_id  ORDER BY 1 DESC");

The problem I'm only getting post id 1 and 2. while there are over 30 posts

I want to get all posts and comments for each post in a single while loop.

How can I do it ?

  • 写回答

2条回答 默认 最新

  • douyu1656 2016-05-24 04:02
    关注

    Change your join to a LEFT join instead of an INNER join.

    The difference being that a LEFT join will use null when there's no comments, where as INNER will only give you the rows that have comments.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器