<?
namespase Query;
$fo = function(){
$id = 30;
$query = Query::sql()
->select('t1.id,t1.name,t2.info AS descriptor')
->from('table1 AS t1')
->leftJoin('table3 AS t3')->on('t1.id=t3.id')->on('t2.reg',true),
->from('table2 AS t2')
->order('t1.name')
->where('t1.active=TRUE')
->where('t1.deleted',null,'!=')
->where('t1.id=t2.id')
->where('t1.type',[1,2])
->where((new \Query\Rules('or'))
->rule("t2.mode='add' ")
->rule('t2.info','описание','%')
)
->where('t1.id',function() use($id){
return Query::sql()->select('id')->from('table3')->where('chain_id',$id);
})
->limit('2,20');
return $query->Query;
};
echo $fo();
?>
Функция вернёт такой запрос
SELECT
t1.id,
t1.name,
t2.info AS descriptor
FROM
table1 AS t1
LEFT JOIN table3 AS t3 ON t1.id=t3.id AND t2.reg=TRUE,table2 AS t2,
table2 AS t2
WHERE
t1.active=TRUE AND
t1.deleted IS NOT NULL AND
t1.id=t2.id AND
t1.type IN (1,2) AND
(t2.mode='add' OR t2.info LIKE '%описание') AND
t1.id IN (
SELECT
id
FROM table3
WHERE chain_id IN (30)
)
ORDER BY t1.name
LIMIT 2 OFFSET 20