简体   繁体   English

从数据库中的不同表中获取记录

[英]Fetching records from different tables in the database

My application has a facebook-like stream that displays updates of various types. 我的应用程序有一个类似于Facebook的流,其中显示了各种类型的更新。 So it will show regular posts (from the "posts" table), and events (from the "events" tables) table and so on. 因此,它将显示常规帖子(来自“ posts”表)和事件(来自“ events”表)表,依此类推。

The problem is I have no idea how to fetch these records from different tables since they have different columns. 问题是我不知道如何从不同的表中获取这些记录,因为它们具有不同的列。 Shall I query the database multiple times and then organize the data in PHP? 我应该多次查询数据库,然后用PHP组织数据吗? if so, how? 如果是这样,怎么办? I'm not sure how I should approach this. 我不确定该如何处理。

Your help is much appreciated :) 非常感谢您的帮助 :)

Ideally you'd simply use a JOIN to obtain data from multiple tables in one query. 理想情况下,您只需使用一个JOIN即可在一个查询中从多个表中获取数据。 However, without knowing more about your table schemas it's hard to provide any useful specifics. 但是,在不了解表模式的情况下,很难提供任何有用的细节。 (It most likely won't be possible unless you've factored this in from the beginning that said.) (除非您从一开始就考虑了这一点,否则极有可能无法实现。)

As such, you might also want to create a generic "meta" table that provides information for each of the posts/events in a common format, and provides a means to link to the relevant table. 这样,您可能还想创建一个通用的“元”表,以通用格式提供每个帖子/事件的信息,并提供一种链接到相关表的方法。 (ie: It would contain the "parent" type and ID.) You could then use this meta table as the source for the "updates" stream and drill down to the approriate content as required. (即:它将包含“父”类型和ID。)然后,您可以使用此元表作为“更新”流的源,并根据需要向下钻取适当的内容。

Unless the events and post are related to each other, then you'd probably query them separately, even if they show up on the same page. 除非事件和发布彼此相关,否则即使它们显示在同一页面上,您也可能要分别查询它们。

You're not going to want to use JOIN just for the sake of it. 您不会仅仅为了它而使用JOIN。 Only if there is a foreign key relationship. 仅当存在外键关系时。 If you don't know what that is, then you don't have one. 如果您不知道那是什么,那么您就没有一个。

If the data tables are related to each other you can generally get the data back in a single query using some combination of JOINs and UNIONs. 如果数据表相互关联 ,则通常可以使用JOIN和UNION的某种组合在单个查询中获取数据。 For a better answer, however, you'll have to post the structure of your data tables and a sample of what (combined) records you need for the website. 为了获得更好的答案,您必须发布数据表的结构以及网站所需记录(合并记录)的样本。

If you don't know the columns, you can get the table meta-data and find out what the columns represent and their corresponding data types. 如果您不知道列,则可以获取表元数据,并找出列代表什么以及它们对应的数据类型。
If you know which columns, you can select from the multiple tables or even use nested selects or joins to get the data out. 如果您知道哪些列,则可以从多个表中进行选择,甚至可以使用嵌套的选择或联接来获取数据。

Join the tables on user_id ie 在user_id上加入表,即

Select * from posts p
left join status_updates su on p.user_id = su.user_id
limit 25;

or if your tables differ too much then play with a temporary table first 或者如果您的桌子相差太大,那么请先使用临时桌子

create table tmp_updates
(
 select user_id, p.id as update_id, 'post' as update_type, p.text from posts;
);

insert into table tmp_updates
(
 select user_id, su.id as update_id, 'status' as update_type, su.text from status_updates;
);

Select * from tmp_updates
where user_id = '...'
limit 25;

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

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