简体   繁体   English

在sql中链接两个表以获取动态数据

[英]Linking two tables in sql for dynamic data

I have two tables, one containing user information (name, address, postcode, email etc) and the other containing user preferences (style of house, size of house, value of house, etc). 我有两个表,一个表包含用户信息(名称,地址,邮政编码,电子邮件等),另一个表包含用户首选项(房屋样式,房屋大小,房屋价值等)。

The first tables information is inputted on a certain webpage, and the data is inserted into the table with no issues upon pressing the submit button. 在特定网页上输入第一个表格信息,然后按提交按钮将数据毫无问题地插入表格中。 I have a hidden auto-incrementing column for the unique id of that user. 对于该用户的唯一ID,我有一个隐藏的自动递增列。 I then did exactly the same for the second table on a second form on the next webpage. 然后,我在下一个网页的第二个表单上对第二个表进行了完全相同的操作。

How can I ensure that the id column in the first table always matches the id in the second column? 如何确保第一张表中的id列始终与第二列中的id匹配? If a user was to complete the first webpage and submit the information, but then not complete the second page, there would be a discrepancy in the two unique id's and the two sets of data would no longer match up for future users of the site. 如果用户要完成第一个网页并提交信息,而后又不完成第二个页面,则两个唯一ID会有差异,并且两组数据将不再适合该站点的将来用户。

Im sure there is a very easy way to link the two tables depending on the user at that time but as im new to this language and databasing im having trouble figuring it out!! 我确定当时有一个非常简单的方法来链接两个表,具体取决于用户,但是作为这种语言和数据库的新手,我很难弄清楚它!

(If it helps, im using Dreamweaver with PHP coding on the website to link to the sql server, with a local server set up using XAMPP.) (如果有帮助,我可以在网站上使用Dreamweaver和网站上的PHP编码来链接到sql服务器,并使用XAMPP设置本地服务器。)

Im sure there is a very easy way to link the two tables depending on the user 我确定有一种非常容易的方法来链接两个表,具体取决于用户

The best way of linking users between tables is to use an identifier for that user (let's call it a user ID). 在表之间链接用户的最好方法是使用该用户的标识符(我们称其为用户ID)。 Generate an ID for the user when they complete the first table. 用户完成第一个表时为其生成一个ID。 Next when they move on to complete more details about the property, recall the ID generated for the user and submit that. 接下来,当他们继续完成有关该属性的更多详细信息时,请回忆为用户生成的ID并提交。

use table structure like this: 使用这样的表结构:

user (unique_autoincrement_id, other_fields)
user_properties (unique_autoincrement_id, user_id_linked_to_user_table, other_fields)

do NOT use two different autoincrement fields to identify one user - you definitely will have big and nasty issues with it. 不要使用两个不同的自动增量字段来标识一个用户-您肯定会遇到麻烦大的问题。

When a user submit some data that goes saved somewhere in a table in your database, you may want to have a column like "user_id" that contains the id of the user that have generated the row. 当用户提交一些要保存在数据库表中某处的数据时,您可能希望使用诸如“ user_id”之类的列,其中包含已生成行的用户的ID。

Plus you may want to search on google for "foreign key" and "inner join". 另外,您可能想在Google上搜索“外键”和“内部联接”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM