1-
21# PHP MySQL Query Builder
32
43This library required when you need to create MySQL Query using classes and arrays. This Library only for creating Select Query For MySQL Databse.
@@ -7,9 +6,6 @@ i have used SqlFormatter Library built by Jeremy Dorn <jeremy@jeremydorn.com> to
76
87Feel Free to comment and if any one want to countribute then please contact me Hardeep Singh < hardeepvicky1@gmai.com > .
98
10-
11-
12-
139## Installation
1410
1511Install Library using composer
@@ -18,8 +14,6 @@ Install Library using composer
1814 composer require hardeep-vicky/php-query-builder
1915```
2016
21-
22-
2317## Basic Usage/Examples
2418
2519- First include composer's autoload file.
@@ -54,20 +48,19 @@ echo $q;
5448
5549```
5650
57-
5851## Output
59- ```
52+
6053SELECT
6154 C.*
6255FROM
6356 ` countries ` AS C
6457WHERE
6558 (C.name like '%india%')
66- ```
59+
6760## Use with Complex Conditions
61+
6862``` php
6963require_once './vendor/autoload.php';
70-
7164use HardeepVicky\QueryBuilder\QuerySelect;
7265use HardeepVicky\QueryBuilder\Table;
7366use HardeepVicky\QueryBuilder\Join;
@@ -87,31 +80,23 @@ $querySelect->setWhere(
8780$q = $querySelect->get();
8881
8982echo $q;
90-
91- ```
92- ## Output
93- ```
94- SELECT
95- C.*
96- FROM
97- `countries` AS C
98- WHERE
99- (C.name like '%india%')
10083```
84+
10185## Output
86+
10287```
103- SELECT
104- C.*
105- FROM
106- `countries` AS C
107- WHERE
88+ SELECT
89+ C.*
90+ FROM
91+ `countries` AS C
92+ WHERE
10893 (
109- region = 'Asia'
94+ region = 'Asia'
11095 AND (
111- C.name like '%india%'
112- OR C.name like '%pakistan%'
96+ C.name like '%india%' OR C.name like '%pakistan%'
11397 )
114- )
98+ )
99+
115100```
116101## Example with Join
117102
@@ -135,6 +120,7 @@ $q = $querySelect->get();
135120echo SqlFormatter::format($q);
136121
137122```
123+
138124## Output
139125
140126```
@@ -159,7 +145,7 @@ class Join
159145 const INNER = 'INNER JOIN';
160146 const LEFT = 'LEFT JOIN';
161147 const OUTER = 'OUTER JOIN';
162-
148+
163149 /**
164150 * @param String $join_type
165151 * @param Table $table
@@ -169,17 +155,21 @@ class Join
169155 {
170156```
171157
172- And we call
158+ And we call
159+
173160```
174161$join_state->field("name");
175162```
163+
176164this statement make select ` name ` field of states table. This function ` field() ` has three option
165+
177166```
178167$join_state->field("name"); //output S.name
179168$join_state->field("name", "state_name"); //output S.name as state_name
180169$join_state->field("name", null, true); //output S.name as S__name
181170
182171```
172+
183173Above options also avialable in QuerySelect class
184174
185175```
@@ -188,7 +178,6 @@ $querySelect->field("name", "country_name"); //output C.name as country_name
188178$querySelect->field("name", null, true); //output C.name as C__name
189179```
190180
191-
192181you can set also no field as below
193182
194183```
@@ -201,7 +190,6 @@ $querySelect->noField();
201190
202191this statement make no field selction in query
203192
204-
205193## Multiple Join
206194
207195``` php
@@ -217,12 +205,14 @@ $join_state->field("name");
217205$querySelect->join($join_state);
218206
219207$querySelect->field("id");
220- $querySelect->field("name");
221-
208+ $querySelect->field("name");
209+
222210$q = $querySelect->get();
223211
224212```
213+
225214## Output
215+
226216```
227217SELECT
228218 Country.id,
@@ -250,8 +240,8 @@ $join_state->field("name");
250240$querySelect->join($join_state);
251241
252242$querySelect->field("id");
253- $querySelect->field("name");
254-
243+ $querySelect->field("name");
244+
255245$q = $querySelect->get();
256246```
257247
268258 LEFT JOIN `states` ON `states`.country_id = `countries`.id
269259 LEFT JOIN `cities` ON `cities`.state_id = `states`.id
270260```
261+
271262## Join With Condition
263+
272264We have two options here
273265
274266``` php
@@ -281,6 +273,7 @@ $join_city->setWhere(
281273```
282274
283275## Output
276+
284277```
285278SELECT
286279 C.name,
@@ -294,13 +287,14 @@ FROM
294287 )
295288```
296289
297- ### we can concat aw string as below
290+ ### we can concat raw where string as below
298291
299292``` php
300293$join_city->addRawWhere("AND (S.name = City.name)");
301294```
302295
303296### Output
297+
304298```
305299SELECT
306300 C.name AS C__name,
@@ -309,8 +303,7 @@ SELECT
309303FROM
310304 `countries` AS C
311305 INNER JOIN `states` AS S ON S.country_id = C.id
312- INNER JOIN `cities` AS City ON City.state_id = S.id
313- AND (S.name = City.name)
306+ INNER JOIN `cities` AS City ON City.state_id = S.id AND (S.name = City.name)
314307```
315308
316309## Another Examples are below
@@ -325,7 +318,7 @@ $querySelect->join($join_state);
325318
326319$querySelect->field("name");
327320
328- $querySelect->addCustomField("SUM (S.id) AS state_count");
321+ $querySelect->addCustomField("COUNT (S.id) AS state_count");
329322
330323$querySelect->groupBy("countries.id");
331324
@@ -334,17 +327,21 @@ $querySelect->setHaving(Condition::init("AND")->add("state_count", 5, ">"));
334327$querySelect->order("state_count", "desc");
335328
336329$querySelect->setLimit(10);
337-
330+
338331$q = $querySelect->get();
339332
340333echo SqlFormatter::format($q);
341334```
342335
336+ In above example we use custom field ` $querySelect->addCustomField("COUNT(S.id) AS state_count"); `
337+ and conditions in having clause ` $querySelect->setHaving(Condition::init("AND")->add("state_count", 5, ">")); `
338+
343339## Output
340+
344341```
345342SELECT
346343 `countries`.name,
347- SUM (S.id) AS state_count
344+ COUNT (S.id) AS state_count
348345FROM
349346 `countries`
350347 LEFT JOIN `states` AS S ON S.country_id = `countries`.id
@@ -358,7 +355,6 @@ LIMIT
358355 10
359356```
360357
361-
362358``` php
363359$querySelect = new QuerySelect(new Table("countries", "C"));
364360
@@ -382,11 +378,12 @@ $querySelect->groupBy("C.id");
382378$querySelect->order("same_name_count", "DESC");
383379
384380$querySelect->setHaving(Condition::init("AND")->add("C__name", "%india%", "like"));
385-
381+
386382$q = $querySelect->get();
387383```
388384
389385## Output
386+
390387```
391388SELECT
392389 C.name AS C__name,
@@ -402,4 +399,4 @@ HAVING
402399 (C__name like '%india%')
403400ORDER BY
404401 same_name_count DESC
405- ```
402+ ```
0 commit comments