Tag Archive for search

MySQL search

Word searching

1.

SELECT * FROM TABLE WHERE MATCH (`field`) AGAINST ('Keyword')

(Fastest)

2.

SELECT * FROM TABLE WHERE MATCH (`field`) AGAINST ('+Keyword' IN BOOLEAN MODE)

(Fast)

3.

SELECT * FROM TABLE WHERE RLIKE '(^| +)Keyword($| +)'

OR

SELECT * FROM TABLE WHERE
RLIKE '([[:space:]]|[[:<:]])Keyword([[:space:]]|[[:>:]])'

(Slow)
Contains searching

1.

SELECT * FROM TABLE WHERE MATCH (`field`) AGAINST ('Keyword*' IN BOOLEAN MODE)

(Fastest)

2.

SELECT * FROM TABLE WHERE FIELD LIKE 'Keyword%'

(Fast)

3.

SELECT * FROM TABLE WHERE MATCH (`field`) AGAINST ('*Keyword*' IN BOOLEAN MODE)

(Slow)

4.

SELECT * FROM TABLE WHERE FIELD LIKE '%Keyword%'

(Slow)
Recordsets

1.

SELECT SQL_CALC_FOUND_ROWS * FROM TABLE WHERE Condition LIMIT 0, 10
SELECT FOUND_ROWS()

(Fastest)

2.

SELECT * FROM TABLE WHERE Condition LIMIT 0, 10
SELECT COUNT(PrimaryKey) FROM TABLE WHERE Condition

(Fast)

3.

$result = mysql_query("SELECT * FROM table", $link);
$num_rows = mysql_num_rows($result);

(Very slow)
Joins

Use an INNER JOIN when you want the joining table to only have matching records that you specify in the join. Use LEFT JOIN when it doesn’t matter if the records contain matching records or not.

SELECT * FROM products
INNER JOIN suppliers ON suppliers.SupplierID = products.SupplierID

Returns all products with a matching supplier.

SELECT * FROM products
LEFT JOIN suppliers ON suppliers.SupplierID = products.SupplierID
WHERE suppliers.SupplierID IS NULL

Returns all products without a matching supplier.

source

Scrape Google from the command line

perl -e "$i=0;while($i<1000){sleep 1; open(WGET,qq/|xargs lynx -dump/);printf WGET qq{http://www.google.com/search?q=site:onemorebug.com&hl=en&start=$i&sa=N},$i+=10}" | grep "//[^/]*onemorebug.com/"

source

Find doubled IDs in the DOM

var allTags = document.body.getElementsByTagName('*');
var ids = {};
var doubledIds = [];
for (var tg = 0; tg< allTags.length; tg++) {
var tag = allTags[tg];
if (tag.id) {
if (! ids[tag.id]) {
ids[tag.id] = true;
} else {
doubledIds.push(tag.id);
}
}
}
alert('doubled ids: ' + doubledIds);

source

Search and replace text in a string

var newText:String = "The rain in Spain falls mainly on the plain";
var splitText:Array = newText.split("Spain");
newText = splitText.join("Barcelona");
// outputs "The rain in Barcelona falls mainly on the plain"

source

search

//creation of the index:
//this means that when searching the string will be looked for in the the 3 fields specified within the brakets.

ALTER TABLE fulltext_sample ADD FULLTEXT(username,first_name,last_name)

//searching:
//this searches any word where $mystring is contained (that's why the *) in the 3 cols
SELECT * FROM fulltext_sample WHERE MATCH(username,first_name,last_name) AGAINST('*".$mystring."*' IN BOOLEAN MODE);

source

search and replace across multiple files with Perl

#print the result of search-and-replace to the terminal
perl -pe 's/bart/milhouse/g' test.html

#search-and-replace, with backup
#leave the suffix off of -i to overwrite
perl -i.bak -pe 's/bart/milhouse/g' test.html

#echo the number of lines in a file
perl -lne 'END { print $t } @w = /(w+)/g; $t += @w' test.html

#cat file with line numbers
# -p prints $_ each iteration
perl -pe '$_ = "$. = $_"' test.html

# recursive search-and-replace, only on shells that support file globs
perl -i.bak -pe 's{bart}{milhouse}' **/*html

source

My search book with acts as ferret

  def search
# Book.rebuild_index
@page_title = 'Search books'
return unless request.post?
@books = Book.find_by_contents(params[:q]) if params[:q]
# flash.now[:notice] = 'No results.' unless @books.size > 0
end

source

Nombre de ligne dans un fichier contenant un mot

grep "REPLACE" hitech_deee.sql | wc -l

source

inArray prototype

Array.prototype.inArray = function(valeur) {
for (var i in this) { if (this[i] === valeur) return i; }
return -1;
}

/*
in_array() renvois la clef si la valeur est présente, sinon, renvoie -1.  exemple : 

var t=new Array("coucou","test","plop");
alert(    t.in_array("test")   ); //affichera 1
alert(    t.in_array("bobo")   ); //affichera -1
*/

source

Recherche d’un fichier

find . -name "*dedicace*" -print
locate nomfichier

source