[英]How use UNION and Zend DB if number of columns different
I need get information from two different tables.我需要从两个不同的表中获取信息。 Like:
喜欢:
table1 (col1, col2, col3, col4)表 1(col1、col2、col3、col4)
table2 (col1, col2)表 2 (col1, col2)
I try use UNION for it.我尝试使用 UNION 。 If type:
如果键入:
$res = $db->query('
SELECT col1, col2, col3, col4 FROM table1
UNION SELECT col1, col2, null, null FROM table2
')->fetchAll();
Code work correct, I had result.代码工作正确,我有结果。
But I need use Zend_DB_Adapter, so I try like但我需要使用 Zend_DB_Adapter,所以我试试
$select2 = $db->select()->from(['t2' => 'table2'], ['col1', 'col2', new Zend_Db_Expr("null"), new Zend_Db_Expr('null')]);
$select1 = $db->select()->from(['t1'=>'table1'], ['col1', 'col2', 'col3', 'col4']);
$select = $db->select()->union([$select1, $select2], Zend_Db_Select::SQL_UNION);
$res = $db->query($select)->fetchAll();
This attempt got only 3 columns and last had false for each rows.这次尝试只有 3 列,最后每行都为 false。
If change it like:如果将其更改为:
$db->select()->from(['t2' => 'table2'], ['col1', 'col2', null, null]);
I had error: "each UNION query must have the same number of columns"我有错误:“每个 UNION 查询必须有相同数量的列”
Please, help me use Zend_DB and UNION with number of columns different.请帮助我使用不同列数的 Zend_DB 和 UNION。
It's work这是工作
$select2 = $db->select()->from(['t2' => 'table2'], ['col1', 'col2', 'col3' => new Zend_Db_Expr("null"), 'col4' => new Zend_Db_Expr('null')]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.