Skip to content

Commit 1f5b5cc

Browse files
committed
readme updated
1 parent 8afd3fa commit 1f5b5cc

File tree

1 file changed

+43
-46
lines changed

1 file changed

+43
-46
lines changed

README.md

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# PHP MySQL Query Builder
32

43
This 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

87
Feel 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

1511
Install 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+
6053
SELECT
6154
C.*
6255
FROM
6356
`countries` AS C
6457
WHERE
6558
(C.name like '%india%')
66-
```
59+
6760
## Use with Complex Conditions
61+
6862
```php
6963
require_once './vendor/autoload.php';
70-
7164
use HardeepVicky\QueryBuilder\QuerySelect;
7265
use HardeepVicky\QueryBuilder\Table;
7366
use HardeepVicky\QueryBuilder\Join;
@@ -87,31 +80,23 @@ $querySelect->setWhere(
8780
$q = $querySelect->get();
8881

8982
echo $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();
135120
echo 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+
176164
this 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+
183173
Above 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-
192181
you can set also no field as below
193182

194183
```
@@ -201,7 +190,6 @@ $querySelect->noField();
201190

202191
this 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
```
227217
SELECT
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

@@ -268,7 +258,9 @@ FROM
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+
272264
We have two options here
273265

274266
```php
@@ -281,6 +273,7 @@ $join_city->setWhere(
281273
```
282274

283275
## Output
276+
284277
```
285278
SELECT
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
```
305299
SELECT
306300
C.name AS C__name,
@@ -309,8 +303,7 @@ SELECT
309303
FROM
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

340333
echo 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
```
345342
SELECT
346343
`countries`.name,
347-
SUM(S.id) AS state_count
344+
COUNT(S.id) AS state_count
348345
FROM
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
```
391388
SELECT
392389
C.name AS C__name,
@@ -402,4 +399,4 @@ HAVING
402399
(C__name like '%india%')
403400
ORDER BY
404401
same_name_count DESC
405-
```
402+
```

0 commit comments

Comments
 (0)