如何在连接查询后将 "FIRST" table 的列移动到结果的最后?
How to move a column of the "FIRST" table to the last in the result after a join query?
我用的是zend,但是问题的答案完全可以在mysql.
中给出
protected function _getView() {
$gatewayTable = new Emailer_Model_DbTable_Gateway();
$select = $this->_getDb()->select()
->from(
array(
'gatewayTable' => $gatewayTable->getTableName()
),
array(
'id',
'name',
'requireAuth',
'smtphost',
'smtpPort',
'sendLimit',
'inError',
'lastError',
'errorCount'
)
);
return $select;
}
$authTable = new Emailer_Model_DbTable_Auth();
$select = $this->_getView();
$select
->join(
array(
'authTable' => $authTable->getTableName()
),
'gatewayTable.id = authTable.idEmailerGateway',
array(
"authId" => "id",
'username',
'fromAddress',
'fromName',
"password" => "encryptedPwd",
'sentCount',
'lastUsed'
)
);
当我对结果 $select
执行提取时,我希望第一个 table 的 errorCount
列排在 $res
的最后。
$res = $this->_getDb()->query($select)->fetch();
您是说要更改结果中各列的顺序?
在 SQL 中,您只需按需要的顺序 select 列即可。喜欢
SELECT t1.x, t1.y, t2.a, t2.b, t1.z
FROM t1
JOIN t2
对于Zend_Db有columns()
方法。
https://framework.zend.com/manual/1.10/en/zend.db.select.html
段落 向现有 FROM 或 JOIN 添加列 table
There may be cases where you wish to add columns to an existing FROM or JOIN table after those methods have been called. The columns() method allows you to add specific columns at any point before the query is executed. You can supply the columns as either a string or Zend_Db_Expr or as an array of these elements. The second argument to this method can be omitted, implying that the columns are to be added to the FROM table, otherwise an existing correlation name must be used.
使用 Example #11 使用 columns() 方法添加列的示例
// Build this query:
// SELECT p."product_id", p."product_name"
// FROM "products" AS p
$select = $db->select()
->from(array('p' => 'products'), 'product_id')
->columns('product_name');
// Build the same query, specifying correlation names:
// SELECT p."product_id", p."product_name"
// FROM "products" AS p
$select = $db->select()
->from(array('p' => 'products'), 'p.product_id')
->columns('product_name', 'p');
// Alternatively use columns('p.product_name')
我用的是zend,但是问题的答案完全可以在mysql.
中给出protected function _getView() {
$gatewayTable = new Emailer_Model_DbTable_Gateway();
$select = $this->_getDb()->select()
->from(
array(
'gatewayTable' => $gatewayTable->getTableName()
),
array(
'id',
'name',
'requireAuth',
'smtphost',
'smtpPort',
'sendLimit',
'inError',
'lastError',
'errorCount'
)
);
return $select;
}
$authTable = new Emailer_Model_DbTable_Auth();
$select = $this->_getView();
$select
->join(
array(
'authTable' => $authTable->getTableName()
),
'gatewayTable.id = authTable.idEmailerGateway',
array(
"authId" => "id",
'username',
'fromAddress',
'fromName',
"password" => "encryptedPwd",
'sentCount',
'lastUsed'
)
);
当我对结果 $select
执行提取时,我希望第一个 table 的 errorCount
列排在 $res
的最后。
$res = $this->_getDb()->query($select)->fetch();
您是说要更改结果中各列的顺序?
在 SQL 中,您只需按需要的顺序 select 列即可。喜欢
SELECT t1.x, t1.y, t2.a, t2.b, t1.z
FROM t1
JOIN t2
对于Zend_Db有columns()
方法。
https://framework.zend.com/manual/1.10/en/zend.db.select.html
段落 向现有 FROM 或 JOIN 添加列 table
There may be cases where you wish to add columns to an existing FROM or JOIN table after those methods have been called. The columns() method allows you to add specific columns at any point before the query is executed. You can supply the columns as either a string or Zend_Db_Expr or as an array of these elements. The second argument to this method can be omitted, implying that the columns are to be added to the FROM table, otherwise an existing correlation name must be used.
使用 Example #11 使用 columns() 方法添加列的示例
// Build this query:
// SELECT p."product_id", p."product_name"
// FROM "products" AS p
$select = $db->select()
->from(array('p' => 'products'), 'product_id')
->columns('product_name');
// Build the same query, specifying correlation names:
// SELECT p."product_id", p."product_name"
// FROM "products" AS p
$select = $db->select()
->from(array('p' => 'products'), 'p.product_id')
->columns('product_name', 'p');
// Alternatively use columns('p.product_name')