这是我的代码。我想从两列中选择一个结果。感谢。
$messages = (new \yii\db\Query())
->select(['idsender as idotherguy'])
->from('messages')
->where(['idreceiver' => Yii::$app->user->identity->id])
->all();
$messages2 = (new \yii\db\Query())
->select(['idreceiver as idotherguy'])
->from('messages')
->where(['idsender' => Yii::$app->user->identity->id])
->all();
$messages->union($messages2);
经过这里的建议,我也试过了......
$messages = (new \yii\db\Query())
->select(['idsender as idotherguy'])
->from('messages')
->where(['idreceiver' => Yii::$app->user->identity->id]);
$messages2 = (new \yii\db\Query())
->select(['idreceiver '])
->from('messages')
->where(['idsender' => Yii::$app->user->identity->id]);
$messages->union($messages2);
$messages->all();
我得到了查询对象
yii\db\Query Object
( [select] =>排列 ( [0] => idsender as idotherguy )
[selectOption] =>
[distinct] =>
[from] => Array
(
[0] => messages
)
[groupBy] =>
[join] =>
[having] =>
[union] => Array
(
[0] => Array
(
[query] => yii\db\Query Object
(
[select] => Array
(
[0] => idreceiver as idotherguy
)
[selectOption] =>
[distinct] =>
[from] => Array
(
[0] => messages
)
[groupBy] =>
[join] =>
[having] =>
[union] =>
[params] => Array
(
)
[_events:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] =>
[where] => Array
(
[idsender] => 2
)
[limit] =>
[offset] =>
[orderBy] =>
[indexBy] =>
[emulateExecution] =>
)
[all] =>
)
)
[params] => Array
(
)
[_events:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] =>
[where] => Array
(
[idreceiver] => 2
)
[limit] =>
[offset] =>
[orderBy] =>
[indexBy] =>
[emulateExecution] =>
)
就是这样......我也尝试过回答并用于选择'idreceiver as idotherguy'......但结果是相同的
答案 0 :(得分:1)
您应该避免联合查询中的别名,使用文字选择格式并仅将结果应用于结果
$messages = (new \yii\db\Query())
->select('idsender as idotherguy')
->from('messages')
->where(['idreceiver' => Yii::$app->user->identity->id]);
$messages2 = (new \yii\db\Query())
->select('idreceiver')
->from('messages')
->where(['idsender' => Yii::$app->user->identity->id]);
$messages->union($messages2);
$messages->all();
查看你应该
的结果 foreach($messages as $key=> $value) {
echo $value->idotherguy;
}
或者结果是数组
foreach($messages as $key=> $value) {
echo $value['idotherguy'];
}
尝试以这种方式检查ral sql代码(而不是$ messages-> all();)
var_dump( $messages->createCommand()->sql);
答案 1 :(得分:0)
查看文档http://www.yiiframework.com/doc-2.0/guide-db-query-builder.html#union,您的查询中不应该有$messages = (new \yii\db\Query())
->select(['idsender as idotherguy'])
->from('messages')
->where(['idreceiver' => Yii::$app->user->identity->id]);
$messages2 = (new \yii\db\Query())
->select(['idreceiver as idotherguy'])
->from('messages')
->where(['idsender' => Yii::$app->user->identity->id]);
$messages->union($messages2);
$messages->all();
function getvalHT(sel) {
var selectedFromHours = $('#RoomFromTimeH').val();
var selectedToHours = $('#RoomToTimeH').val();
console.log(selectedFromHours);
console.log(selectedToHours);
var selectedFromMin = $('#RoomFromTimeM').val();
var selectedToMin = $('#RoomToTimeM').val();
if(selectedFromHours > selectedToHours) {
$("#savebutton").show();
}else {
$.alert({
title: 'Time period error!',
content: 'The "From" hours time of ' + $('#RoomFromTimeH').val() + '<BR/><BR/> must be less than<BR/><BR/> the "To" hours time of ' + $('#RoomToTimeH').val() + '.',
icon: 'fa fa-rocket',
animation: 'zoom',
boxWidth: '50%',
closeAnimation: 'zoom',
buttons: {
okay: {
text: 'Try again',
btnClass: 'btn-blue'
}
},
cancel: function () {
$.alert('canceled');
}
});
$("#savebutton").hide(); // any other condition hide the button
$('#RoomFromTimeH').prop('selectedIndex',0);
$('#RoomToTimeH').prop('selectedIndex',0);
$('#RoomFromTimeM').prop('selectedIndex',0);
$('#RoomToTimeM').prop('selectedIndex',0);
}
}
答案 2 :(得分:0)
我改变了这里提供的代码,现在工作正常....
$messages = (new \yii\db\Query())
->select(['idsender as idotherguy'])
->from('messages')
->where(['idreceiver' => Yii::$app->user->identity->id]);
$messages2 = (new \yii\db\Query())
->select(['idreceiver as idotherguy'])
->from('messages')
->where(['idsender' => Yii::$app->user->identity->id]);
$messages = $messages->union($messages2)
->all();
结果现在是带有我想要的值的数组。 排列 ( [0] =&gt;排列 ( [idotherguy] =&gt; 3 )
[1] => Array
(
[idotherguy] => 11
)
[2] => Array
(
[idotherguy] => 10
)
)
感谢帮助男孩:)