Skip to content

Commit a69c571

Browse files
committed
Added content to INSTRUCTIONS.md
1 parent b50b1c3 commit a69c571

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

INSTRUCTIONS.md

+26-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
# The SELECT Statement
1+
# SQL and PHP Query Builder Conversion Guide
22

3-
## FROM Clause
3+
This guide is designed to help AI systems translate SQL queries into PHP Query Builder syntax and vice versa, covering essential SQL clauses and concepts.
4+
5+
## The SELECT Statement
6+
7+
### FROM Clause
48

59
Consider the following SQL statement:
610

@@ -16,7 +20,7 @@ $db->select()
1620
->from('p', 'products');
1721
```
1822

19-
### Using an Alias in the FROM Clause:
23+
#### Using an Alias in the FROM Clause:
2024

2125
There are generally two ways to write this. In the syntax with two parameters, the first parameter is always the alias, and the second parameter is always the table name:
2226

@@ -30,7 +34,7 @@ This is equivalent to
3034
/* ... */ ->from('p', 'products');
3135
```
3236

33-
### Using the FROM Clause Without an Alias:
37+
#### Using the FROM Clause Without an Alias:
3438

3539
If only one parameter is used, it will be interpreted as the table name:
3640

@@ -44,7 +48,7 @@ This is equivalent to
4448
/* ... */ ->from('products');
4549
```
4650

47-
## Using Table Fields
51+
### Using Table Fields
4852

4953
Translate table fields to match the following pattern:
5054

@@ -87,9 +91,9 @@ $db->select()
8791

8892
Here’s the enhanced document with additional sections explaining the use of LEFT JOIN, RIGHT JOIN, INNER JOIN, WHERE, HAVING, ORDER BY, LIMIT, OFFSET, and subselects in SQL, along with corresponding examples in PHP Query Builder.
8993

90-
## JOIN Clauses
94+
### JOIN Clauses
9195

92-
### INNER JOIN
96+
#### INNER JOIN
9397

9498
`INNER JOIN` returns rows when there is a match in both tables. Example SQL:
9599

@@ -103,7 +107,7 @@ PHP Query Builder equivalent:
103107
/* ... */ ->joinInner('t2', 'test2', 't2.test_id = t1.id AND t2.field1 = ?', 123)
104108
```
105109

106-
### LEFT JOIN
110+
#### LEFT JOIN
107111

108112
`LEFT JOIN` returns all rows from the left table, and matched rows from the right table. If there is no match, NULL values are returned for columns from the right table.
109113

@@ -119,7 +123,7 @@ PHP Query Builder equivalent:
119123
->joinLeft('t3', 'test3', 't3.test_id = t1.id')
120124
```
121125

122-
### RIGHT JOIN
126+
#### RIGHT JOIN
123127

124128
`RIGHT JOIN` returns all rows from the right table, and matched rows from the left table. If there is no match, NULL values are returned for columns from the left table. Example SQL:
125129

@@ -133,7 +137,7 @@ PHP Query Builder equivalent:
133137
->joinRight('t4', 'test4', 't4.test_id = t1.id')
134138
```
135139

136-
### Using Subselects in JOINs
140+
#### Using Subselects in JOINs
137141

138142
A subselect can be used as a virtual table in joins. Here’s an example with `RIGHT JOIN`:
139143

@@ -154,7 +158,7 @@ $subSelect = function ($id) use ($mysql) {
154158
$mysql->joinRight('t5', $subSelect(10), 't5.test_id = t1.id');
155159
```
156160

157-
## WHERE Clause
161+
### WHERE Clause
158162

159163
The `WHERE` clause is used to filter records that meet certain conditions. Example SQL:
160164

@@ -170,7 +174,7 @@ $mysql->where('t1.field = ?', 'value');
170174

171175
The value can be a scalar value (including `null`) and can also be an array of values. The Query Builder will automatically translate value-arrays into an according `IN`-Clause.
172176

173-
## HAVING Clause
177+
### HAVING Clause
174178

175179
The `HAVING` clause is used to filter records after aggregation. It typically applies to grouped results. Example SQL:
176180

@@ -184,7 +188,7 @@ PHP Query Builder equivalent:
184188
->having('customer_count > 10')
185189
```
186190

187-
## ORDER BY Clause
191+
### ORDER BY Clause
188192

189193
The ORDER BY clause is used to sort the result set by one or more columns. Example SQL:
190194

@@ -199,7 +203,7 @@ PHP Query Builder equivalent:
199203
->orderBy('t1.field2', 'DESC')
200204
```
201205

202-
## LIMIT Clause
206+
### LIMIT Clause
203207

204208
The LIMIT clause specifies the maximum number of records to return. Example SQL:
205209

@@ -213,7 +217,7 @@ PHP Query Builder equivalent:
213217
->limit(100)
214218
```
215219

216-
## OFFSET Clause
220+
### OFFSET Clause
217221

218222
The OFFSET clause is used in conjunction with LIMIT to skip a certain number of rows before beginning to return rows. Example SQL:
219223

@@ -227,7 +231,7 @@ PHP Query Builder equivalent:
227231
->offset(50)
228232
```
229233

230-
## Using Subselects
234+
### Using Subselects
231235

232236
Subselects (or subqueries) are queries nested within another query, often used to retrieve data for specific conditions.
233237

@@ -254,9 +258,9 @@ $select = $mysql->select()
254258
->joinRight('t5', $subSelect(10), 't5.test_id = t1.id');
255259
```
256260

257-
# The INSERT Statement
261+
## The INSERT Statement
258262

259-
## Basic INSERT
263+
### Basic INSERT
260264

261265
Consider the following SQL statement:
262266

@@ -274,7 +278,7 @@ $db->insert()
274278
->run();
275279
```
276280

277-
### Using Expressions in INSERT
281+
#### Using Expressions in INSERT
278282

279283
To include SQL expressions directly in the INSERT statement, use `addExpr`:
280284

@@ -291,7 +295,7 @@ $db->insert()
291295
->run();
292296
```
293297

294-
## UPSERT (INSERT ON DUPLICATE KEY UPDATE)
298+
### UPSERT (INSERT ON DUPLICATE KEY UPDATE)
295299

296300
For scenarios where you want to insert a new row or update an existing row if a duplicate key is found, use `addOrUpdate` and `addOrUpdateExpr`:
297301

@@ -311,7 +315,7 @@ $db->insert()
311315
->run();
312316
```
313317

314-
### Using Expressions with Parameters in UPSERT
318+
#### Using Expressions with Parameters in UPSERT
315319

316320
To use SQL functions with parameters in UPSERT scenarios, use `addOrUpdateExpr` with placeholders:
317321

@@ -329,7 +333,7 @@ $db->insert()
329333
->run();
330334
```
331335

332-
## UPDATE Part in UPSERT
336+
### UPDATE Part in UPSERT
333337

334338
To specify assignments only in the `UPDATE` part during an UPSERT scenario, use `update` and `updateExpr`:
335339

0 commit comments

Comments
 (0)