简体   繁体   English

如何连接其他表相关的多个表

[英]How to join multiple tables related by other tables

I'm developing a site where people can publicate their houses for rent. 我正在开发一个人们可以出租房屋的网站。 I'm using php 5.2.0 and MySQL 5+ 我正在使用php 5.2.0和MySQL 5+

the publications are stored in a table like this 出版物存储在这样的表格中

ta_publications
+---+-------------+------+
|id |    name     | date |
+---+-------------+------+
| 1 | name_001    |  ... |
| 2 | name_002    |  ... |
| 3 | name_003    |  ... |
+---+-------------+------+

I have diferent publications, which have "features" such as "internet", "made service", "satellite tv", etc. 我有不同的出版物,有“互联网”,“制作服务”,“卫星电视”等“功能”。

These features might change in the future, and I want to be able to add/remove/modify them, so I store them in the database in a table. 这些功能将来可能会发生变化,我希望能够添加/删除/修改它们,因此我将它们存储在表中的数据库中。

ta_features
+---+-------------+
|id | name        |
+---+-------------+
| 1 | Internet    |
| 2 | Wi-Fi       |
| 3 | satelital tv|
+---+-------------+

which are related to the publications using the following table 与使用下表的出版物有关

ta_publication_features
+---+-------------+----------------+
|id |   type_id   | publication_id |
+---+-------------+----------------+
| 1 |      1      |       1        |
| 2 |      2      |       1        |
| 3 |      3      |       1        |
+---+-------------+----------------+

I think it's pretty easy to understand; 我认为这很容易理解; There is a publication called name_001 which have internet, wi-fi and satellite tv. 有一个名为name_001的出版物,有互联网,Wi-Fi和卫星电视。

I have the same data-schema for the images, I store them in this table 我有相同的图像数据模式,我将它们存储在此表中

ta_images
+---+-------------+
|id | src         |
+---+-------------+
| 1 | URL_1       |
| 2 | URL_2       |
| 3 | URL_3       |
+---+-------------+

And use the following table to relate them to the publications 并使用下表将它们与出版物联系起来

ta_publication_images
+---+-------------+----------------+----------+
|id |  img_id     | publication_id |   order  |
+---+-------------+----------------+----------+
| 1 |      1      |       1        |    0     |
| 2 |      2      |       1        |    1     |
| 3 |      3      |       1        |    2     |
+---+-------------+----------------+----------+

the column order gives the order in wich publications should be displayed when listing a single publication. 列顺序给出了在列出单个出版物时应显示的出版物的顺序。

Philipp Reichart provided me with a query that will search and get all the publications that have certain features. Philipp Reichart向我提供了一个查询,可以搜索并获取具有某些功能的所有出版物。 It works for listing the publications, I can't modified it to return me the data I need. 它适用于列出出版物,我无法修改它以返回我需要的数据。

So I figured I'll run that query and get all of the publications that pass the search criteria and then use another query to list them. 所以我想我将运行该查询并获取所有通过搜索条件的出版物,然后使用另一个查询来列出它们。

The listing of these publications shall include all publication data (everything on ta_publications)+ all of it's features + the most important (order 0) image src. 这些出版物的清单应包括所有出版物数据(ta_publications上的所有内容)+所有出版物的特征+最重要的(0阶)图像src。

I could, for every publication, have two simple querys wich will return, separately, the most important image and all the features it has, but when listing 25 publications per page, it'll be 1 search query + (2 querys per publication * 25 publications) = 51 different querys, clearly not very efficient. 我可以为每个出版物提供两个简单的查询,它们将分别返回最重要的图像和所有功能,但是当每页列出25个出版物时,它将是1个搜索查询+(每个出版物有2个查询* 25个出版物)= 51个不同的查询,显然效率不高。

EDIT: 编辑:

My question is, how can I create a SQL query that, given some publication ids, will return: all publication data (everything on ta_publications) + all of it's features + the most important (order 0) image src 我的问题是,如何创建一个SQL查询,给定一些发布ID,将返回:所有发布数据(ta_publications上的所有内容)+所有的功能+最重要的(0阶)图像src

 Select pub.Name as [Publication], f.name as [Feature], i.name as [Image] from ta_publications pub
Join ta_publications_features pf on pf.publication_id = pub.id
Join ta_features f on f.id = pf.type_id
Join ta_publication_images pi on pi.publication_id = pub.id
Join ta_images i on i.id = pi.img_id
Where pub.id = 'appropriate id'
order by i.[order]

If I've followed your structure correctly this should give you the joins you need. 如果我正确地遵循了你的结构,这应该给你你需要的连接。 and you can add desired columns to the result in the select statment by adding .. hope this helps. 并且您可以通过添加将所需列添加到选择参数中的结果..希望这会有所帮助。

You'll get redundant publication and image data with this one, but here is a way to do it with one query: 您将获得有关此文件的冗余发布和图像数据,但这是一种使用一个查询执行此操作的方法:

   SELECT p.id, p.name, p.date,
           f.id, f.name,
           i.id, i.src
    FROM ta_publications p
    JOIN ta_publication_features pf ON p.id = pf.publication_id
    JOIN ta_features f ON f.id = pf.type_id
    JOIN ta_publication_images pi ON p.id = pi.publication_id 
         AND pi.order = 0
    JOIN ta_images i ON i.id = pi.img_id
    WHERE p.id IN (  -- list of publication ids );

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

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