
March 22nd, 2007 by

Pete
Here are 3 different ways to perform a DB select when you want to specify two or more values for a field.
1. Use th OR operator
SQL:
-
SELECT * FROM people
-
WHERE (first_name = 'pete'
-
OR first_name = 'paul');
2. Use a Regular Expression
SQL:
-
SELECT * FROM people
-
WHERE first_name ~'^pete|paul$';
3. Use an array
SQL:
-
SELECT * FROM people
-
WHERE first_name = ANY('{pete,paul}');
Note: These should all work in PostgreSQL, I suspect the SQL may need altering for 2 and 3 to work with MySQL.
# Pete Graham
Posted in Tech, database, regular expressions, sql |
1 Comment »

March 22nd, 2007 by

Pete
If you need to remove elements from an array that have a particular value(s) here’s a neat way of doing it without any looping:
PHP:
-
// our initial array
-
$arr =
Array("blue",
"green",
"red",
"yellow",
"green",
"orange",
"yellow",
"indigo",
"red");
-
-
-
// remove the elements who's values are yellow or red
-
-
-
-
// optionally you could reindex the array
-
-
This is the output from the code above:
Array ( [0] => blue [1] => green [2] => red [3] => yellow [4] => green [5] => orange [6] => yellow [7] => indigo [8] => red )
Array ( [0] => blue [1] => green [4] => green [5] => orange [7] => indigo )
Array ( [0] => blue [1] => green [2] => green [3] => orange [4] => indigo )
Pete Graham xXx
Posted in Tech, arrays, php, programming |
13 Comments »