简体   繁体   English

合并来自2个表的数据?

[英]Combining data from 2 tables?

I've been duplicating my data as as noted by the asterisk * below in Snippet 1 . 我一直在复制我的数据,如下面的Snippet 1中的星号*所示。 This duplication allowed me to run a simple query as such: 这个重复允许我运行一个简单的查询:

"SELECT * FROM tweets ORDER BY time DESC LIMIT 7",

This populates 7 tweets as a default setting. 这会将7条推文填充为默认设置。

However, I want to eliminate these redundant columns. 但是,我想消除这些冗余列。 To do this I need to pull the other fields from the credentials table. 为此,我需要从凭证表中提取其他字段。

How would I run a complex query like this? 我该如何运行这样的复杂查询? Is this feasible? 这可行吗? Is it a good idea? 这是个好主意吗?

Snippet 1 片段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

Each tweet has an h_token associated with it that will be used to add relational data (h_file, name, and picture) 每条推文都有一个与之关联的h_token,用于添加关系数据(h_file,name和picture)

You need to use a join operation, like so: 您需要使用连接操作,如下所示:

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;

This is basically your original query, but adding three columns from the credentials table whenever the h_token from the credential and tweets table match up...... 这基本上是您的原始查询,但只要凭证和推文表中的h_token匹配,就会从凭证表中添加三列......

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

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