SQL:超越 INNER JOIN(由于缺乏更好的标题)

发布于 2024-10-31 03:18:40 字数 1022 浏览 3 评论 0原文

环境:
- PHP 5.3.5
- Oracle 11g 数据库

表名:tips_categories

id   category   picture  
1    a          e.jpg  
2    b        f.jpg  
3    c        g.jpg  

表名:tips

id   tips_categories_id   tip
1    3                       This is a tip.
2    2                       This is another tip.
3    1                       This is yet another tip.

所需结果:

$array_name['tips']
    [0]
        category => a
        picture => e.jpg
        tips
            [0] => This is yet another tip.
    [1]
        category => b
        picture => f.jpg
        tips
            [0] => This is another tip.
    [2]
        category => c
        picture => g.jpg
        tips
            [0] => This is a tip.

编写一个返回所需结果的查询超出了我目前对 SQL 的了解。 INNER JOIN 不会返回所需的结果。如果无法在一次查询中完成, 我想必须使用以下两个查询:

SELECT id,category,picture FROM Tips_categories
SELECT Tip FROM Tips WHERE Tips_categories_id = id

我非常感谢有关如何在一条语句中编写此查询的建议。谢谢 :-)

Environment:
- PHP 5.3.5
- Oracle 11g database

Table Name: tips_categories

id   category   picture  
1    a          e.jpg  
2    b        f.jpg  
3    c        g.jpg  

Table Name: tips

id   tips_categories_id   tip
1    3                       This is a tip.
2    2                       This is another tip.
3    1                       This is yet another tip.

Desired result:

$array_name['tips']
    [0]
        category => a
        picture => e.jpg
        tips
            [0] => This is yet another tip.
    [1]
        category => b
        picture => f.jpg
        tips
            [0] => This is another tip.
    [2]
        category => c
        picture => g.jpg
        tips
            [0] => This is a tip.

Writing one query that returns the desired resuilt is beyond my current knowledge of SQL.
An INNER JOIN does not return the desired result. If it cannot be done in one query,
I guess the following two queries will have to be used instead:

SELECT id,category,picture FROM tips_categories
SELECT tip FROM tips WHERE tips_categories_id = id

I would greatly appreciate suggestions on how to write this query in one statement. Thank you :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

祁梦 2024-11-07 03:18:40

查询不返回分层结果集。所以你想做的事是不可能的。

您可以编写一个简单的内部联接查询,按 id 排序,然后创建您自己迭代结果集的数组,并在每次 id 时向数组中添加一个新条目变化。

查询:

SELECT tips.id,category,picture, tip
  FROM tips_categories
 INNER JOIN tips on (tips_categories_id = id)
 ORDER BY tips.ip

因此,您的结果集将类似于:

 id   category   picture  tip
 1    a          e.jpg    This is yet another tip A01.
 1    a          e.jpg    This is yet another tip A02.
 1    a          e.jpg    This is yet another tip A03.
 2    b          f.jpg    This is another tip B01.
 2    b          f.jpg    This is another tip B02.
 3    c          g.jpg    This is a tip C01.

假设您的 tips 表上有更多条目。

您使用 PHP 进行迭代并在数组上生成所需的结果。它应该非常简单明了。

Queries don't return hierarchical result sets. So what you are trying to do is impossible.

You can write a simple inner join query, order by id, and then create the array your self iterating over the result set and adding a new entry into the array every time id changes.

Query:

SELECT tips.id,category,picture, tip
  FROM tips_categories
 INNER JOIN tips on (tips_categories_id = id)
 ORDER BY tips.ip

So, your result set will be something like:

 id   category   picture  tip
 1    a          e.jpg    This is yet another tip A01.
 1    a          e.jpg    This is yet another tip A02.
 1    a          e.jpg    This is yet another tip A03.
 2    b          f.jpg    This is another tip B01.
 2    b          f.jpg    This is another tip B02.
 3    c          g.jpg    This is a tip C01.

Provided you have more entries on your tips table.

You iterate with PHP and produce the desired result on your array. It should be pretty simple and straightforward.

痴梦一场 2024-11-07 03:18:40

嗯,这个:

SELECT c.id, c.category, c.picture, t.tip
FROM tips_categories AS c
INNER JOIN tips AS t
    ON t.tips_categories_id = c.id

当然会返回您想要的数据,但提示只是第四列,而不是某种层次结构。

您使用什么来根据结果构建数组?

Well this:

SELECT c.id, c.category, c.picture, t.tip
FROM tips_categories AS c
INNER JOIN tips AS t
    ON t.tips_categories_id = c.id

Certainly returns the data you want, but tip will simply be a 4th column, not some kind of hierarchy.

What are you using to build your array(s) from the results?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文