PHP code example of jasny / persist-sql-query

1. Go to this page and download the library: Download jasny/persist-sql-query library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jasny / persist-sql-query example snippets


use Jasny\Persist\SQL\Query;

$query = (new Query("SELECT * FROM foo LEFT JOIN bar ON foo.bar_id = bar.id WHERE active = 1 LIMIT 25"))
    ->where('type', 'bike')
    ->where('price between ? and ?', [10, 20])
    ->page(3);

echo $query; // SELECT * FROM foo LEFT JOIN bar ON foo.bar_id = bar.id WHERE (active = 1) AND (`type` = 'bike') AND (`price` between 10 and 20) LIMIT 25 OFFSET 50

use Jasny\Persist\SQL\Query;

$columns = [
    'ref' => 'ref',
    'man' => 'boy',
    'woman' => 'girl',
    'amount' => 'SUM(z.bucks)'
];

$build = Query::build('mysql');

$select = $build->select()->columns($columns)->from('foo')->innerJoin('z', 'foo.id = z.foo_id')->groupBy('foo.id');
$insert = $build->insert()->into('abc')->columns(array_keys($columns))->set($select)->onDuplicateKeyUpdate();

echo $insert; // INSERT INTO `abc` (`ref`, `man`, `woman`, `amount`)
              //   SELECT `ref` AS `ref`, `boy` AS `man`, `girl` AS `woman`, SUM(`z`.`bucks`) AS `amount` FROM `foo` LEFT JOIN `z` ON `foo`.`id` = `z`.`foo_id` GROUP BY `foo`.id`
              //   ON DUPLICATE KEY UPDATE `ref` = VALUES(`ref`), `man` = VALUES(`man`), `woman` = VALUES(`woman`), `amount` = VALUES(`amount`)