drupal数据库数据获取与多个具有相同列的表

时间:2017-01-04 21:49:20

标签: php database drupal-7

我正在尝试从多个表中获取数据"具有相同的列名"但它并没有给我在drupal中所需的结果。 这是我的代码。

$result = db_query ( "select table1.symbol, table2.symbol, table1.price_sales, table2.price_sales from {table1, table2} where table1.uid = table2.uid" );

while ( $obj = $result->fetch () ) {
    echo ($obj->table1.symbol);  // it doesn't return the results
    echo ($obj->table2.symbol);
}

2 个答案:

答案 0 :(得分:0)

fetch() 不是 DatabaseStatementInterfaceDupral 7.x的方法。试试fetchAllAssoc()fetchAllKeyed()

请参阅DatabaseStatementInterface Documentation

答案 1 :(得分:0)

您应该能够在运行查询时更改任何列的名称:

$result = db_query ( "select table1.symbol as second_symbol, table2.symbol, table1.price_sales, table2.price_sales from {table1, table2} where table1.uid = table2.uid" );

while ( $obj = $result->fetch () ) {
    echo ($obj->table1.second_symbol);
    echo ($obj->table2.symbol);
}