Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.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
Php 如何在多个表到一个表之间创建join语句?_Php_Mysql_Sql_Join - Fatal编程技术网

Php 如何在多个表到一个表之间创建join语句?

Php 如何在多个表到一个表之间创建join语句?,php,mysql,sql,join,Php,Mysql,Sql,Join,首先,我有4个表和列,例如 提要(id、类型、类型\u id) feeds_法线(id、类型、内容) 提要链接(id、类型、标题、链接) feed_youtube(id、类型、标题、链接、描述、图像) feeds_照片(id、类型、链接) “提要类型_id”表是法线、链接、YouTube、照片的匹配/链接“id” 及 “提要类型”的表使用已确定的应联接的表 例如: feeds: id: 1, type: "normal", type_id: 1 id: 2, type: "link", type

首先,我有4个表和列,例如

  • 提要(id、类型、类型\u id)
  • feeds_法线(id、类型、内容)
  • 提要链接(id、类型、标题、链接)
  • feed_youtube(id、类型、标题、链接、描述、图像)
  • feeds_照片(id、类型、链接)
  • “提要类型_id”表是法线、链接、YouTube、照片的匹配/链接“id”

    “提要类型”的表使用已确定的应联接的表

    例如:

    feeds:
    id: 1, type: "normal", type_id: 1
    id: 2, type: "link", type_id: 1
    
    feeds_normals:
    id: 1, type: "normal", content: "this is a test"
    
    feeds_links:
    id: 1, type: "link", title: "This is a title", link: "http://yahoo.com"
    
    id: 1, type: "normal", content: "this is a test", title: NULL, link: NULL
    id: 2, type: "link", content: NULL, title: "This is a title", link: "http://yahoo.com"
    
    结果:

    feeds:
    id: 1, type: "normal", type_id: 1
    id: 2, type: "link", type_id: 1
    
    feeds_normals:
    id: 1, type: "normal", content: "this is a test"
    
    feeds_links:
    id: 1, type: "link", title: "This is a title", link: "http://yahoo.com"
    
    id: 1, type: "normal", content: "this is a test", title: NULL, link: NULL
    id: 2, type: "link", content: NULL, title: "This is a title", link: "http://yahoo.com"
    
    最后


    在这种情况下,如何使用SQL语句编写?

    一般来说,您不能。您描述的结构称为带鉴别器的超类型/子类型(如果您搜索它,您可以在google Books上找到一些描述),虽然在ER图中很容易绘制,但在实际数据库中很难使用。如果可以,考虑切换到其他实现鉴别器的形式,具体地包括一个表中的所有字段。如果无法更改结构,则必须注意编程语言内部的条件并进行两次查询。

    正如leafnode所建议的,最好更改表结构。特别是考虑到您有重复数据(类型在feedtable和子表中声明)


    我建议要么删除feeds表,要么将所有内容映射到一个表(具有可空列)。如果您希望按ID对提要进行排序(正如我通过查看所需结果所假设的那样),后者将是实现这一点的最简单方法。

    这里有些错误,可能是示例或实际数据。下面是超级类型/子类型模型通常的样子:

    匹配数据示例如下:

    Feeds:
    FeedId: 1, type: "normal"
    FeedId: 2, type: "link"
    
    Feeds_Normals:
    FeedId: 1, content: "this is a test"
    
    Feeds_Links:
    FeedId: 2, title: "This is a title", link: "http://yahoo.com"
    
    请注意,子类型表中的
    FeedID
    与超级类型中的匹配。子类型表中的
    Type
    字段是可选的——它允许一个检查约束来强制执行类型不会在子类型表中混合

    查询将类似于:

    select
          f.FeedID
        , n.Content      as NormalsContent
        , y.Title        as YouTubeTitle
        , y.Link         as YouTubeLink
        , y.Description  as YouTubeDescription
        , y.Image        as YouTueImage
        , k.Title        as LinksTitle
        , k.Link         as LinksLink
    from Feeds              as f
    left join Feeds_Normals as n on n.FeedId = f.FeedId
    left join Feeds_Links   as k on k.FeedId = f.FeedId
    left join Feeds_YouTube as y on y.FeedId = f.FeedId ;
    

    在这种情况下,我知道它将复制数据,但如果我将所有内容映射到一个表,映射的表将非常大,因此我尝试存储在不同的表中。@Zeuxis因此选择两个选项。我仍然建议删除“父”提要表,然后向其他表添加额外的时间戳列(如果feeds表ID确实用于对feed进行排序)。一般来说,我认为不按ID对记录进行排序是更好的做法。如果我删除“父”feeds表,并继续在每个表中存储不同的数据。我可以知道如何同时从4个表中提取数据并按时间戳列排序吗?应该使用“union all”语句?在这个图中,它有相同的问题,比如如何使一个SQL语句同时从3个表中获取数据?