SQL:超越内部联接(因为缺少更好的标题)
环境:SQL:超越内部联接(因为缺少更好的标题),sql,oracle,oracle11g,inner-join,Sql,Oracle,Oracle11g,Inner Join,环境: -PHP 5.3.5 -oracle11g数据库 表名:tips\U类别 id category picture 1 a e.jpg 2 b f.jpg 3 c g.jpg id类别图片 1 a e.jpg 2b f.jpg 3CG.jpg 表名:tips id tips_categories_id tip 1 3 This is a tip. 2
-PHP 5.3.5
-oracle11g数据库 表名:tips\U类别 id category picture 1 a e.jpg 2 b f.jpg 3 c g.jpg id类别图片 1 a e.jpg 2b f.jpg 3CG.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. id提示\u类别\u id提示 1 3这是一个提示。 这是另一个提示。 3 1这是另一个提示。 预期结果: $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. $array_name['tips'] [0] 类别=>a 图片=>e.jpg 提示 [0]=>这是另一个提示。 [1] 类别=>b 图片=>f.jpg 提示 [0]=>这是另一个提示。 [2] 类别=>c 图片=>g.jpg 提示 [0]=>这是一个提示。 编写一个返回所需重新过滤的查询超出了我目前对SQL的了解。 内部联接不会返回所需的结果。如果不能在一个查询中完成, 我猜必须使用以下两个查询: 从提示和类别中选择id、类别、图片
从tips\u categories\u id=id的tips中选择tip
我将非常感谢关于如何在一个语句中编写此查询的建议。谢谢:-)查询不会返回分层结果集。所以你想做的是不可能的 您可以编写一个简单的内部联接查询,按
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进行迭代,并在数组上生成所需的结果。它应该非常简单和直接。查询不会返回分层结果集。所以你想做的是不可能的 您可以编写一个简单的内部联接查询,按
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
当然,返回您想要的数据,但tip将只是第4列,而不是某种层次结构
您使用什么从结果中构建阵列?那么:
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
当然,返回您想要的数据,但tip将只是第4列,而不是某种层次结构
您使用什么从结果构建数组?为什么内部联接不能提供所需的结果?这看起来像是常规联接。您尝试了什么sql,得到了什么?每个条目目前只有一个提示是巧合吗?也许您可以更新示例以显示希望重复的位置。为什么内部联接不能提供所需的结果?这看起来像是常规联接。您尝试了什么sql,得到了什么?每个条目目前只有一个提示是巧合吗?也许你可以更新这些例子,以显示你希望重复的地方。谢谢大家,感谢你们令人惊讶的快速、有用的回复!我最终使用了我原来的内部连接语句,并使用结果集以我想要的格式构建了一个数组。很抱歉,我的问题浪费了StackOverflow数据库的空间。我的大脑感觉好像被翻了个底朝天。现在是吃东西的时候了:-)谢谢你们,所有人,感谢你们令人惊讶的快速、有益的回复!我最终使用了我原来的内部连接语句,并使用结果集以我想要的格式构建了一个数组。很抱歉,我的问题浪费了StackOverflow数据库的空间。我的大脑感觉好像被翻了个底朝天。现在该吃饭了:-)