您的位置:首页 > 其它

Phalapi 中Union和Union All的用法

2017-11-28 00:00 148 查看
有时候在进行数据库查询的时候会用到union查询,但是查询的时候会发现两个limit只有一个是有效的,如下查询

select * from table where status = 0 limit 10
union
select * from table where status = 1 limit 30

这样的语句实际的查询效果是这样的,如下:

(select * from table where status = 0 limit 10)
union
(select * from table where status = 1) limit 20

如果想让limit有效,必须要把limit放到括号当中才行。

Phalapi中notorm的写法是,如下:

public function getSogouGuojiList() {
$result = array();
$query = DI()->notorm->table->select('id,title,content')
->where('status =?', 0)->order('id desc')
->limit(10);
$query2 = DI()->notorm->table->select('id,title,content')
->where('status =?', 0)->order('id desc')
->limit(20);
foreach ($query->union($query2) as $row) {
$result[] = $row;
}
return $result;
}

打印sql语句,链接后面跟上&__sql__=1就可以了。

完整的链接是:http://localhost/PhalApi/Public/?service=Default.index&__sql__=1(根据自己的配置环境修改成自己的url地址)

注:如果有不对的地方还请多多指教。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PhalApi union查询