首页 > 解决方案 > 我可以使用 PHP 获得一个带有一个 SQL 选择的单个数组吗

问题描述

我正在做一个博客网站,并希望在我的页面中显示我的所有文章。这些文章有作者和翻译(因为这是一个多语言网站)。

这是我的桌子

table : articles
rows :
- id
- release_date
- author_id

table : author
rows :
- id
- firstname
- lastname

table : translations
rows :
- id
- article_id
- translator_id
- translation_date
- language
- content
- title

我想显示所有信息,所以我想拥有这种类型的数组

articles => array(
    0 => array(
        id => ...
        release_date => ...
        author => array (
            firstname => ...
            lastname => ...
        )
        translations => array (
            0 => array (
                translator => array (
                    firstname => ...
                    lastname => ...
                )
                translation_date => ...
                language => ...
                content => ...
                title => ...
            )
            1 => array(
                ...
            )
            ...
        )
    )
    1 => array(
        ...
    )
    ...
)

我想要所有文章,按release_date作者顺序排序,每种语言的最后一个翻译,对于每一个翻译,我都想要翻译

这可能在一个查询中吗?因为现在我正在做一些循环,服务器要花很多时间来回答(每篇文章用 2 种不同的语言大约需要 5 次调用,想象一下超过 100 篇文章......)

标签: phpsql

解决方案


推荐阅读