PHPackages                             fotano/sql-query-builder - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. fotano/sql-query-builder

ActiveLibrary[Database &amp; ORM](/categories/database)

fotano/sql-query-builder
========================

 A fork of nilportugues/sql-query-builder that fixes some bugs. An elegant lightweight and efficient SQL QueryInterface BuilderInterface supporting bindings and complicated query generation.

1.5.0(10y ago)0231MITPHPPHP &gt;=5.5

Since Jul 1Pushed 4y ago1 watchersCompare

[ Source](https://github.com/fotano/php-sql-query-builder)[ Packagist](https://packagist.org/packages/fotano/sql-query-builder)[ Docs](http://nilportugues.com)[ RSS](/packages/fotano-sql-query-builder/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (4)Versions (15)Used By (0)

SQL Query Builder
=================

[](#sql-query-builder)

[![Build Status](https://camo.githubusercontent.com/7a0e7ad659cc6318d4c49a419c5e9c5549e665fd2d403fceecdaecf687be3dca/68747470733a2f2f7472617669732d63692e6f72672f6e696c706f727475677565732f7068702d73716c2d71756572792d6275696c6465722e737667)](https://travis-ci.org/nilportugues/php-sql-query-builder) [![Coverage Status](https://camo.githubusercontent.com/ae8679346b400e2e273bee89d338184e8fa8710488a5bc7fcaf9b11f10651f04/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6e696c706f727475677565732f73716c2d71756572792d6275696c6465722e737667)](https://coveralls.io/r/nilportugues/sql-query-builder) [![Scrutinizer Code Quality](https://camo.githubusercontent.com/209c6cf044439db68b1bfab77e6ca32cb549b82c1a4d6c6d50026f204d9117d9/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6e696c706f727475677565732f73716c2d71756572792d6275696c6465722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/nilportugues/sql-query-builder/?branch=master) [![SensioLabsInsight](https://camo.githubusercontent.com/206a88af1ec9b6bc227a48ee940aa462f311e2ac967425da4793a8389df24f06/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f38396563313030332d343232372d343361322d383433322d3637613966633264336261332f6d696e692e706e67)](https://insight.sensiolabs.com/projects/89ec1003-4227-43a2-8432-67a9fc2d3ba3) [![Latest Stable Version](https://camo.githubusercontent.com/ce944f522137007692f572012d64a8bd1d73cd86484082a2e29de21ecfcd4678/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f73716c2d71756572792d6275696c6465722f762f737461626c65)](https://packagist.org/packages/nilportugues/sql-query-builder) [![Total Downloads](https://camo.githubusercontent.com/b3a3fa333e1fadb9b1025e7785d4bc6a07e5c6df4391abc2eca763f4640a7a27/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f73716c2d71756572792d6275696c6465722f646f776e6c6f616473)](https://packagist.org/packages/nilportugues/sql-query-builder) [![License](https://camo.githubusercontent.com/8be27fb459def8b41cffdcc37c2263cd3deeeb864ec529d5c68e32d83419560f/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f73716c2d71756572792d6275696c6465722f6c6963656e7365)](https://packagist.org/packages/nilportugues/sql-query-builder)[![Donate](https://camo.githubusercontent.com/7b6de155df30b37b25eb5fec52f9213680c3dbf067dfb7d7e2850ac4096c7d05/68747470733a2f2f7777772e70617970616c6f626a656374732e636f6d2f656e5f55532f692f62746e2f62746e5f646f6e6174655f534d2e676966)](https://paypal.me/nilportugues)

An elegant lightweight and efficient SQL Query Builder with fluid interface SQL syntax supporting bindings and complicated query generation. **Works without establishing a connection to the database.**

- [1. Installation](#block1)
- [2. The Builder](#block2)
    - [2.1. Generic Builder](#block2.1)
    - [2.2. MySQL Builder](#block2.2)
    - [2.3. Human Readable Output](#block2.3)
- [3. Building Queries](#block3)
    - [3.1. SELECT Statement](#block3.1)
        - [3.1.1. Basic SELECT statement](#block3.1.1)
        - [3.1.2. Aliased SELECT statement](#block3.1.2)
        - [3.1.3. SELECT with WHERE statement](#block3.1.3)
        - [3.1.4. Complex WHERE conditions](#block3.1.4)
        - [3.1.5. JOIN &amp; LEFT/RIGHT/INNER/CROSS JOIN SELECT statements](#block3.1.5)
        - [3.1.6. COUNT rows](#block3.1.6)
    - [3.2. INSERT Statement](#block3.2)\* [3.2.1. Basic INSERT statement](#block3.2.1)
    - [3.3. UPDATE Statement](#block3.3)
        - [3.3.1. Basic UPDATE statement](#block3.3.1)
        - [3.3.2. Elaborated UPDATE statement](#block3.3.2)
    - [3.4. DELETE Statement](#block3.4)
        - [3.4.1. Empty table with DELETE statement](#block3.4.1)
        - [3.4.2. Basic DELETE statement](#block3.4.2)
        - [3.4.3. Elaborated DELETE statement](#block3.4.3)
    - [3.5. INTERSECT Statement](#block3.5)
    - [3.6. MINUS Statement](#block3.6)
    - [3.7. UNION Statement](#block3.7)
    - [3.8. UNION ALL Statement](#block3.8)
- [4. Advanced Quering](#block4)
    - [4.1. Filtering using WHERE](#block4.1)
        - [4.1.1. Changing WHERE logical operator](#block4.2)
        - [4.1.2. Writing complicated WHERE conditions](#block4.2)
    - [4.3. Grouping with GROUP BY and HAVING](#block4.3)
        - [4.3.1 Available HAVING operators](#block4.3.1)
    - [4.4. Changing HAVING logical operator](#block4.4)
    - [4.5. Columns as SELECT statements](#block4.5)
    - [4.6. Columns being Values](#block4.6)
    - [4.7. Columns using FUNCTIONS](#block4.7)
- [5. Commenting queries](#block5)
- [6. Quality Code](#block6)
- [7. Author](#block7)
- [8. License](#block8)

1. Installation [↑](#index_block)
---------------------------------

[](#1-installation-)

The recommended way to install the SQL Query Builder is through [Composer](http://getcomposer.org). Run the following command to install it:

```
php composer.phar require nilportugues/sql-query-builder
```

2. The Builder [↑](#index_block)
--------------------------------

[](#2-the-builder-)

The SQL Query Builder allows to generate complex SQL queries standard using the `SQL-2003` dialect (default) and the `MySQL` dialect, that extends the `SQL-2003` dialect.

### 2.1. Generic Builder [↑](#index_block)

[](#21-generic-builder-)

The Generic Query Builder is the default builder for this class and writes standard SQL-2003.

**All column aliases are escaped using the `'` sign by default.**

#### Usage:

[](#usage)

```
