dongyan1841 2014-01-19 17:00
浏览 111
已采纳

如何将一行MySQL数据库表链接到另一个表中的另一行

I know it makes little sense... and i'm new to using MySQL... What i'm trying to do here is, link one tables row to another tables row... for an example there are two tables.. one table is for user registration and same table is used for login as well... and the next table is for user posts.. like status updates and all...

here is how i want it...

user_log_info:-
id ( primary )
firstname
lastname
username
email
password

posts:-
id ( primary )
userposts
posted_by
date_post

so as you can see, i want the user_log_info tables username to be automatically copied to posts posted_by row... And i have no idea how i can archive this...

  • 写回答

1条回答 默认 最新

  • doutangguan2460 2014-01-19 17:09
    关注

    You haven't given nearly enough information to give a full answer, but I'll do my best with what you've given.

    Tables

    +-----------------+ +-----------------+
    | users_log_info  | | posts           |
    +-----------------+ +-----------------+
    | int ID (primary)| | int ID (primary)|
    +-----------------+ | int posted_by   |
                        +-----------------+
    

    (I left off fields that are irrelevant to what you seem to want to do, I'm just simplifying it)

    posted_by is an unofficial foreign key, or referencing the primary key of another table.

    To insert, what you can do is along the lines of this:

    INSERT INTO posts(...., posted_by) VALUES (...., user.ID)
    

    Where .... is referencing all of your other information to insert

    Then, to find information on someone who posted something:

    SELECT * FROM users_log_info WHERE ID = Post.posted_by
    

    Or if you want to find all posts by a user:

    SELECT * FROM posts WHERE posted_by = user.ID
    

    So, if Bob, who is User ID 3 wants to post "Hi", you might be able to do:

    INSERT INTO posts(content, posted_by) VALUES('Hi', bob.ID)
    

    And then when you are outputting the post you might do this:

    post = (however you choose the post to put on the page)
    userPosted = SELECT * FROM users_log_info WHERE ID = post.posted_by
    
    print post.content + " posted by: " userPosted.Name
    

    Essentially, the field "posted_by" is, to "posts" an arbitrary number, but you know that it links to, or references, a user. It does that by referencing "ID", which is the primary key of users_log_info, so that when you want to get information from users_log_info, is all you need to do is select the entry which has the ID that corresponds to "posted_by". I do recommend naming it something like posterID, however, for easier identification.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。