[英]SQL: Beyond INNER JOIN (for lack of a better title)
环境:
-PHP 5.3.5
-Oracle 11g数据库
表名称:tips_categories
id category picture 1 a e.jpg 2 b f.jpg 3 c g.jpg
表名称:提示
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不返回所需的结果。 如果不能在一个查询中完成,那么我猜将不得不使用以下两个查询:
从tips_categories中选择ID,类别,图片
从提示中选择提示WHERE tips_categories_id = id
我非常感谢有关如何在一个语句中编写此查询的建议。 谢谢 :-)
查询不会返回分层结果集。 因此,您试图做的事情是不可能的。
您可以编写一个简单的内部联接查询,按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进行迭代,并在数组上产生所需的结果。 它应该非常简单明了。
好吧:
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
当然会返回您想要的数据,但是小费只会是第4列,而不是某种层次结构。
您正在使用什么从结果构建阵列?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.