Adodb:Postgresql从两个表中选择但返回一组结果
我是 SQL 新手,有两个保存数据的表(我使用的是 Adodb)。它们都有将它们连接在一起的键,因此我想从第一个表中选择一个名称(如果该名称在第二个表中具有父 ID)。我正在使用:
$db->GetCol("SELECT x_ast.name FROM x_ast, x_ast_tree WHERE x_ast_tree.parent='$parent_id'");
这会返回一个包含正确数据的数组,但它在其中出现了两次。 (我假设是因为我要求它来自两个表):
Array
(
[0] => Trash
[1] => Users
[2] => admin
[3] => Trash
[4] => Users
[5] => admin
)
如何根据另一个表中的数据从一个表中选择一个字段,但只返回一组结果?我做错了什么?
I am new to SQL, and have two tables that hold data(I am using Adodb). They both have keys that connect them together, so I wanted to select a name from the first table if that has a parent id in the second table. I am using:
$db->GetCol("SELECT x_ast.name FROM x_ast, x_ast_tree WHERE x_ast_tree.parent='$parent_id'");
This returns an array with the correct data, but it is in there twice. (I assume because I asked it to come from two tables):
Array
(
[0] => Trash
[1] => Users
[2] => admin
[3] => Trash
[4] => Users
[5] => admin
)
How can I select a field from one table based on the data in another table, but only return one set of results? What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您没有设置连接两个表的标准,因此它正在执行交叉连接。
The problem is that you haven't set a criterion for joining the two tables, so it's doing a cross join.
查看表之间的联接
Have a look at Joins Between Tables