-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy path05-ordering.php
More file actions
209 lines (176 loc) · 5.99 KB
/
05-ordering.php
File metadata and controls
209 lines (176 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/**
* Example 05: Ordering Results
*
* Demonstrates various ways to order query results
*/
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../helpers.php';
use tommyknocker\pdodb\helpers\Db;
$db = createExampleDb();
$driver = getCurrentDriver($db);
echo "=== Ordering Examples (on $driver) ===\n\n";
// Setup
recreateTable($db, 'products', [
'id' => 'INTEGER PRIMARY KEY AUTOINCREMENT',
'name' => 'TEXT',
'category' => 'TEXT',
'price' => 'REAL',
'stock' => 'INTEGER',
'rating' => 'REAL'
]);
$products = [
['name' => 'Laptop Pro', 'category' => 'Electronics', 'price' => 1299.99, 'stock' => 15, 'rating' => 4.8],
['name' => 'Mouse Wireless', 'category' => 'Electronics', 'price' => 29.99, 'stock' => 50, 'rating' => 4.5],
['name' => 'Keyboard Mechanical', 'category' => 'Electronics', 'price' => 89.99, 'stock' => 30, 'rating' => 4.7],
['name' => 'Monitor 27"', 'category' => 'Electronics', 'price' => 399.99, 'stock' => 20, 'rating' => 4.6],
['name' => 'Desk Standing', 'category' => 'Furniture', 'price' => 499.99, 'stock' => 5, 'rating' => 4.9],
['name' => 'Chair Ergonomic', 'category' => 'Furniture', 'price' => 299.99, 'stock' => 8, 'rating' => 4.8],
['name' => 'Lamp LED', 'category' => 'Furniture', 'price' => 49.99, 'stock' => 25, 'rating' => 4.4],
];
$db->find()->table('products')->insertMulti($products);
echo "✓ Inserted " . count($products) . " products\n\n";
// Example 1: Single column ordering (ASC)
echo "1. Order by price (ascending)...\n";
$byPriceAsc = $db->find()
->from('products')
->select(['name', 'price'])
->orderBy('price', 'ASC')
->get();
foreach ($byPriceAsc as $p) {
echo " • {$p['name']}: \${$p['price']}\n";
}
echo "\n";
// Example 2: Single column ordering (DESC)
echo "2. Order by rating (descending)...\n";
$byRatingDesc = $db->find()
->from('products')
->select(['name', 'rating'])
->orderBy('rating', 'DESC')
->limit(3)
->get();
foreach ($byRatingDesc as $p) {
echo " • {$p['name']}: {$p['rating']} ⭐\n";
}
echo "\n";
// Example 3: Multiple columns (chained calls)
echo "3. Order by category ASC, then price DESC (chained)...\n";
$multipleChained = $db->find()
->from('products')
->select(['name', 'category', 'price'])
->orderBy('category', 'ASC')
->orderBy('price', 'DESC')
->get();
foreach ($multipleChained as $p) {
echo " • [{$p['category']}] {$p['name']}: \${$p['price']}\n";
}
echo "\n";
// Example 4: Array with explicit directions
echo "4. Order by category DESC, rating ASC (array with explicit directions)...\n";
$arrayExplicit = $db->find()
->from('products')
->select(['name', 'category', 'rating'])
->orderBy(['category' => 'DESC', 'rating' => 'ASC'])
->get();
foreach ($arrayExplicit as $p) {
echo " • [{$p['category']}] {$p['name']}: {$p['rating']} ⭐\n";
}
echo "\n";
// Example 5: Array with default direction
echo "5. Order by stock and name (array with default DESC)...\n";
$arrayDefault = $db->find()
->from('products')
->select(['name', 'stock'])
->orderBy(['stock', 'name'], 'DESC')
->get();
foreach ($arrayDefault as $p) {
echo " • {$p['name']}: {$p['stock']} in stock\n";
}
echo "\n";
// Example 6: Comma-separated string with directions
echo "6. Order by category ASC, price DESC, name ASC (comma-separated)...\n";
$commaSeparated = $db->find()
->from('products')
->select(['name', 'category', 'price'])
->orderBy('category ASC, price DESC, name ASC')
->get();
foreach ($commaSeparated as $p) {
echo " • [{$p['category']}] {$p['name']}: \${$p['price']}\n";
}
echo "\n";
// Example 7: Comma-separated with partial directions
echo "7. Order by price DESC, name (comma-separated, name defaults to ASC)...\n";
$commaPartial = $db->find()
->from('products')
->select(['name', 'price'])
->orderBy('price DESC, name')
->limit(5)
->get();
foreach ($commaPartial as $p) {
echo " • {$p['name']}: \${$p['price']}\n";
}
echo "\n";
// Example 8: Order by expression (CASE WHEN)
echo "8. Order by custom priority (Electronics first, then Furniture)...\n";
$byPriority = $db->find()
->from('products')
->select(['name', 'category', 'price'])
->orderBy(Db::raw("CASE WHEN category = 'Electronics' THEN 1 ELSE 2 END"))
->orderBy('price', 'DESC')
->get();
foreach ($byPriority as $p) {
echo " • [{$p['category']}] {$p['name']}: \${$p['price']}\n";
}
echo "\n";
// Example 9: Combining different methods
echo "9. Mixed ordering methods (array + chained)...\n";
$mixed = $db->find()
->from('products')
->select(['name', 'category', 'stock'])
->orderBy(['category' => 'ASC'])
->orderBy('stock', 'DESC')
->get();
foreach ($mixed as $p) {
echo " • [{$p['category']}] {$p['name']}: {$p['stock']} in stock\n";
}
echo "\n";
// Example 10: Order by multiple columns with limit and offset
echo "10. Paginated results (order by rating DESC, limit 3, offset 1)...\n";
$paginated = $db->find()
->from('products')
->select(['name', 'rating'])
->orderBy('rating', 'DESC')
->limit(3)
->offset(1)
->get();
foreach ($paginated as $p) {
echo " • {$p['name']}: {$p['rating']} ⭐\n";
}
echo "\n";
// Example 11: DISTINCT - Get unique categories
echo "11. DISTINCT - Unique categories...\n";
$categories = $db->find()
->from('products')
->select(['category'])
->distinct()
->orderBy('category')
->get();
echo " Available categories:\n";
foreach ($categories as $cat) {
echo " • {$cat['category']}\n";
}
echo "\n";
// Example 12: DISTINCT with multiple columns
echo "12. DISTINCT - Unique category + high stock combinations...\n";
$uniqueCombinations = $db->find()
->from('products')
->select(['category', 'stock'])
->where('stock', 10, '>')
->distinct()
->orderBy('category')
->orderBy('stock')
->get();
foreach ($uniqueCombinations as $combo) {
echo " • {$combo['category']}: {$combo['stock']} in stock\n";
}
echo "\nAll ordering examples completed!\n";