简体   繁体   English

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

[英]SQL: Beyond INNER JOIN (for lack of a better title)

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

Table Name: tips_categories 表名称: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. 编写一个返回所需结果的查询超出了我目前对SQL的了解。 An INNER JOIN does not return the desired result. INNER JOIN不返回所需的结果。 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 从tips_categories中选择ID,类别,图片
SELECT tip FROM tips WHERE tips_categories_id = id 从提示中选择提示WHERE tips_categories_id = id

I would greatly appreciate suggestions on how to write this query in one statement. 我非常感谢有关如何在一个语句中编写此查询的建议。 Thank you :-) 谢谢 :-)

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. 您可以编写一个简单的内部联接查询,按id排序,然后在结果集上进行自我迭代,并在每次id更改时将新条目添加到数组中,从而创建一个数组。

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. 如果您在tips表上有更多条目。

You iterate with PHP and produce the desired result on your array. 您使用PHP进行迭代,并在数组上产生所需的结果。 It should be pretty simple and straightforward. 它应该非常简单明了。

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. 当然会返回您想要的数据,但是小费只会是第4列,而不是某种层次结构。

What are you using to build your array(s) from the results? 您正在使用什么从结果构建阵列?

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

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