SQL:超越 INNER JOIN(由于缺乏更好的标题)
环境:
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查询不返回分层结果集。所以你想做的事是不可能的。
您可以编写一个简单的内部联接查询,按
id
排序,然后创建您自己迭代结果集的数组,并在每次id
时向数组中添加一个新条目变化。查询:
因此,您的结果集将类似于:
假设您的
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 timeid
changes.Query:
So, your result set will be something like:
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.
嗯,这个:
当然会返回您想要的数据,但提示只是第四列,而不是某种层次结构。
您使用什么来根据结果构建数组?
Well this:
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?