duanbipu7601 2015-12-29 09:21
浏览 53

如何将一个表id值提供给另一个表

I have a two tables in my database. One is "blog" second is "comment".

Blog table have this structure id, title, text, writer_id, created_at, updated_at Comment table have this structure id, blog_id, text, commenter_id, created_at, updated_at

I want to get id value from blog table as in comment table in the form of blog_id . How can i get the same value.

Example:

One blog posted. blog table have these values. id: 1 title: hi i m blogger text: how are you writer_id: 5 (same User id) created_at:25 feb updated_at: 25 feb Blog posted . One commenter comes and comment on this post. Value should be comes as like this in comment table id=1 blog_id:1 text: ok i know created_at:25 feb updated_at: 25 feb

id of blog table and blog_id of comment table should be same. How can i do this with query or php code?.

  • 写回答

2条回答 默认 最新

  • dtsc1684 2015-12-29 09:26
    关注

    Try This Query

    SELECT b.id, c.`text`,..... FROM blog b
    LEFT JOIN comment c
    ON b.id = c.blog_id 
    ORDER BY commenter_id DESC ;
    

    if you want for perticular blog so add WHERE CONDITION

     SELECT b.id, c.`text`,.... FROM blog b
        LEFT JOIN comment c
        ON b.id = c.blog_id 
        b.id = 'Your ID'
        ORDER BY commenter_id DESC ;
    
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效