简体   繁体   English

MySQL表JOIN建议

[英]MySQL Table JOIN Advice

I wondering what the best advise would be to approach a couple of points. 我想知道最好的建议是接近两点。 I have a working database where I am able to register new users, create what I call documents and also categories. 我有一个正常工作的数据库,可以在其中注册新用户,创建所谓的文档以及类别。 The tables are set out as the following: 这些表如下所示:

Users Tables
    `id` int(11) NOT NULL,
      `username` text NOT NULL,
      `password` varchar(64) NOT NULL,
      `psalt` text NOT NULL


Documents Table
`doc_id` int(11) NOT NULL,
  `doc_title` varchar(50) NOT NULL,
  `doc_content` text NOT NULL,
  `doc_created` datetime NOT NULL,
  `doc_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP

Categories Table
`cat_id` int(11) NOT NULL,
  `cat_title` varchar(32) NOT NULL

So when I login as my 1 user currently which is Admin - which two tables join and do I need another table to join them? 因此,当我当前以1个用户身份登录时,这是Admin-哪个两个表联接,我是否需要另一个表将它们联接? I am little unclear on how these become related as I tried using the tool to relate them in phpMyadmin (Not the best tool but it what I have to work with). 当我尝试使用该工具在phpMyadmin中将它们关联起来时,我不清楚它们是如何关联的(不是最好的工具,但我必须使用它)。 the reason behind this is because if I am logged in as 'Admin' or another user and create a doc I want it to say it was published by that user. 这背后的原因是因为如果我以“管理员”或其他用户身份登录并创建文档,我希望它说它是由该用户发布的。

Also, in regards to the documents, they will belong to a category. 此外,关于文件,它们将属于一个类别。 But I am not sure where the association is. 但是我不确定该协会在哪里。 The end result ultimately is that there with will be a number of users who can see different categories and depending on the category are the docs they see. 最终的最终结果是,将有许多用户可以看到不同的类别,并且根据类别,他们可以看到文档。

So, going by your advice I have managed to filter through some data and make it show with SQL. 因此,根据您的建议,我设法过滤了一些数据,并使其与SQL一起显示。

SELECT user_login.id,user_login.username, doc_list.doc_title FROM user_login, doc_list WHERE user_login.id=user_id

I have added a new column in the doc_list table called 'user_id' and this is my relation as to who published it. 我在doc_list表中添加了一个名为“ user_id”的新列,这是我与谁发布它的关系。 Now, I manually entered the user_id into the fields to make sure I got some results back, but how I am from my php (see below) going to push the user id into the table so its automated? 现在,我手动在字段中输入了user_id以确保返回了一些结果,但是我是如何从我的php(请参见下文)中将用户ID推送到表中的,从而使其自动化?

<?php

if(isset($_POST["submit"])){
$hostname='localhost';
$username='######';
$password='######';

try {

$dbh = new PDO("mysql:host=$hostname;dbname=######",$username,$password);

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line

$sql = "INSERT INTO doc_list (doc_title, doc_content, doc_created) VALUES ('".$_POST["doc_title"]."','".$_POST["doc_content"]."', NOW() )";


if ($dbh->query($sql)) {
    header ('Location: ../docList.php');
}
else{
}

$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}

}
?>

您需要将属于用户表用户ID放在“ 文档表 ”中,以提供“它是由该用户发布的”,然后将“用户表”与“文档表”结合在一起。

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

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