php-sql-query-builder icon indicating copy to clipboard operation
php-sql-query-builder copied to clipboard

Across table conditions

Open prasad83 opened this issue 7 years ago • 3 comments

I would like to know how to build the query like below that has conditions across tables.

SELECT TableA.ColA, TableB.ColB
FROM TableA
INNER JOIN TableB ON TableA.id=TableB.id
WHERE (TableA.ColX = TableB.ColX OR TableA.ColY > TableB.ColY) AND (TableA.ColZ < TableB.ColZ)

prasad83 avatar Jun 21 '17 04:06 prasad83

Is there a simpler way than this?

use NilPortugues\Sql\QueryBuilder\Syntax\Column;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
$select = new Select("TableA");
$select->setColumns(array("ColA"));
$select->innerJoin("TableB", "id", "id", array("ColB"));
$where = $select->where();
$where->subWhere()
    ->equals("ColX", new Column("ColX", "TableB"))
    ->greaterThan("ColY", new Column("ColY", "TableB"));
$where->subWhere()
    ->lessThan("ColZ", new Column("ColZ", "TableB"));
use NilPortugues\Sql\QueryBuilder\Builder\MySqlBuilder;
$builder = new MySqlBuilder();
echo $builder->writeFormatted($select) . "\n";

prasad83 avatar Jun 21 '17 10:06 prasad83

not for the time being

nilportugues avatar Jun 21 '17 16:06 nilportugues

Simpler way would be raw mysql query @prasad83

marcus-hiles avatar Mar 29 '19 11:03 marcus-hiles