开发者

Adodb: Postgresql select from two tables but return one set of results

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?


The problem is that you haven't set a criterion for joining the two tables, so it's doing a cross join.

SELECT x_ast.name
FROM x_ast
INNER JOIN x_ast_tree
  ON x_ast.somefield=x_ast_tree.somefield
WHERE x_ast_tree.parent='$parent_id'


Have a look at Joins Between Tables

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜