简体   繁体   English

使用什么类型的Join?

[英]What type of Join to use?

I've got a core table and and 3 tables that extend the 'core' table in different ways. 我有一个核心表和3个表以不同的方式扩展'核心'表。

I'm working with MLS data and I have a 'common' table that contains information common to all mls listings and then a table that has specifically "residential" information, one for "commercial",etc... I have been using mls number to join a single table when I know a listing when the property type is known, but for searching I want to join all of them and have the special fields available for search criteria (not simply searching the common table). 我正在使用MLS数据,我有一个'通用'表,其中包含所有mls列表共有的信息,然后是一个具有“住宅”信息的表,一个用于“商业”等...我一直在使用mls当我知道属性类型已知时我知道列表时加入单个表的数字,但是对于搜索我希望加入所有这些并且具有可用于搜索条件的特殊字段(而不是简单地搜索公用表)。

What type of join will give me a dataset that will contain all listings (including the extended fields in the idx tables) ? 什么类型的连接会给我一个包含所有列表的数据集(包括idx表中的扩展字段)?

For each Common table record there is a single corresponding record in ONLY ONE of the idx tables. 对于每个公用表记录, 只有一个 idx表中有一个相应的记录。

                     ___________
                    |           |
                    |  COMMON   |
                    |           |
                    |___________|
                         _|_   
                          |
       ___________________|_____________________
     _|_                 _|_                   _|_
 _____|_____         _____|______           ____|______ 
|           |       |            |         |           |
|   IDX1    |       |   IDX2     |         |   IDX3    |
|           |       |            |         |           |
|___________|       |____________|         |___________|

If you want everything in one row, you can use something like this format. 如果你想要一行中的所有内容,你可以使用这种格式。 Basically it gives you all the "Common" fields, then the other fields if there is a match otherwise NULL : 基本上它给你所有的“公共”字段,然后是其他字段,如果有匹配,否则为NULL

SELECT  Common.*,
        Idx1.*,
        Idx2.*,
        Idx3.*
FROM Common
LEFT JOIN Idx1
    ON Idx1.MLSKey = Common.MLSKey
LEFT JOIN Idx2
    ON Idx2.MLSKey = Common.MLSKey  
LEFT JOIN Idx3
    ON Idx3.MLSKey = Common.MLSKey

Bear in mind it's better to list out fields than to use the SELECT * whenever possible... 请记住,最好列出字段,而不是尽可能使用SELECT * ...

Also I'm assuming MySQL syntax is the same as SQL Server, which is what I use. 另外我假设MySQL语法与SQL Server相同,这就是我使用的。

I have a similar set up of tables where the table 'jobs' is the core table. 我有一个类似的表,其中表'jobs'是核心表。

I have this query that selects certain elements from each of the other 2 tables: 我有这个查询,从其他两个表中的每一个表中选择某些元素:

SELECT jobs.frequency, twitterdetails.accountname, feeds.feed
FROM jobs
JOIN twitterdetails ON twitterdetails.ID = jobs.accountID
JOIN feeds ON jobs.FeedID = feeds.FeedID 
WHERE jobs.username ='".$currentuser."';");

So, as you can see, no specific JOIN, but the linking fields defined. 因此,正如您所看到的,没有特定的JOIN,但定义了链接字段。 You'd probably just need an extra JOIN line for your set up. 您可能只需要一个额外的JOIN行来进行设置。

Ugly solution / poor attempt / may have misunderstood question: 丑陋的解决方案/糟糕的尝试/可能有误解的问题:

SELECT common.*,IDX1.field,NULL,NULL FROM COMMON 
LEFT JOIN IDX1 ON COMMON.ID = IDX1.ID
WHERE TYPE="RESIDENTIAL"
UNION ALL
SELECT common.*,NULL,IDX2.field,NULL FROM COMMON 
LEFT JOIN IDX2 ON COMMON.ID = IDX2.ID
WHERE TYPE="RESIDENTIAL"
UNION ALL
SELECT common.*,NULL,NULL,IDX3.field FROM COMMON
LEFT JOIN IDX3 ON COMMON.ID = IDX3.ID
WHERE TYPE="INDUSTRIAL"

Orbit is close. 轨道很接近。 Use inner join, not left join. 使用内连接,而不是左连接。 You don't want common to show up in the join if it does not have a row in idx. 如果在idx中没有行,则不希望common出现在join中。

You MUST union 3 queries to get the proper results assuming each record in common can only have 1 idx table. 你必须联合3个查询来获得正确的结果,假设每个共同的记录只能有1个idx表。 Plug in "NULL" to fill in the columns that each idx table is missing so they can be unioned. 插入“NULL”以填充每个idx表缺失的列,以便它们可以联合。

BTW your table design is good. BTW你的桌子设计很好。

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

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