Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 合并来自两个表的数据?_Mysql - Fatal编程技术网

Mysql 合并来自两个表的数据?

Mysql 合并来自两个表的数据?,mysql,Mysql,我一直在复制我的数据,如下面代码片段1中星号*所示。这种重复允许我运行一个简单的查询: "SELECT * FROM tweets ORDER BY time DESC LIMIT 7", 这将填充7条推文作为默认设置 但是,我想消除这些冗余列。为此,我需要从凭证表中提取其他字段 我如何运行这样一个复杂的查询?这是否可行?这是个好主意吗 片段1 Table 1 - tweets (7) id h_token h_file *remove and pull from credential

我一直在复制我的数据,如下面代码片段1中星号
*
所示。这种重复允许我运行一个简单的查询:

"SELECT * FROM tweets ORDER BY time DESC LIMIT 7",
这将填充7条推文作为默认设置

但是,我想消除这些冗余列。为此,我需要从凭证表中提取其他字段

我如何运行这样一个复杂的查询?这是否可行?这是个好主意吗

片段1

Table 1 - tweets (7)

id
h_token
h_file    *remove and pull from credentials
picture   *remove and pull from credentials    
name      *remove and pull from credentials
tweet
time 



Table 2 -  credentials (12)


id
name
email
h_pass
picture
privacy
h_token
h_file
special
page
pane
remember

每个tweet都有一个与之关联的h_标记,用于添加关系数据(h_文件、名称和图片)

您需要使用连接操作,如下所示:

SELECT tweets.*, credentials.h_file, credentials.picture, credentials.name 
    FROM tweets JOIN credentials ON tweets.h_token=credentials.h_token 
    ORDER BY time DESC LIMIT 7;

这基本上是您的原始查询,但是只要来自凭证和tweets表的h_令牌匹配,就会从凭证表添加三列

当然。这就是关系数据库的优势所在。这样,如果您更新凭证中的一行,则不必更新推文中的一些大数字或行。