tags:

views:

63

answers:

3

Hello guys,

I have a table contains 300 locations name (Pris, London,...) is there a way (query) to select locations from specified text.

for example:

" Transport for London (TfL) is in talks with its American, Australian and European partners about issuing a single contactless card for Paris, New York".

I want a query to get:

  • London
  • Paris
  • New York

I tried: SELECT name FROM coutries WHERE name IN ("Transport for London (TfL) is in talks with its American, Australian and European partners about issuing a single contactless card for Paris, New York")

but no chance!, can anyone help please?

+3  A: 
SELECT name 
FROM coutries 
WHERE ("Transport for London (TfL) is in talks with its American, Australian and European partners about issuing a single contactless card for Paris, New York") LIKE CONCAT('%', name, '%');

IN does a completely different thing.

Alin Purcaru
I think he wants the city names from a sentence.
fabrik
and what does my query do?
Alin Purcaru
+1 @Alin, although the "Transport for London..." phrase should be in single quotes, not double quotes.
Mark Bannister
A string is a sequence of bytes or characters, enclosed within either single quote (“'”) or double quote (“"”). http://dev.mysql.com/doc/refman/5.1/en/string-syntax.html . Is there a difference? (except ANSI_QUOTES)
Alin Purcaru
It was ANSI_QUOTES I had in mind.
Mark Bannister
work like a charm :) Thank you Alin :)
A: 

The only way to achieve it this way is to use a like %name% in your SQL - You can loop through your names to build the where clause if you need to and I would also suggest adding the stirng to a variable instead of repeating it like I have:

SELECT name FROM countries WHERE ('Transport for London (TfL) is in talks with its American, Australian and European partners about issuing a single contactless card for Paris, New York') like '%London%' or ('Transport for London (TfL) is in talks with its American, Australian and European partners about issuing a single contactless card for Paris, New York') like '%Paris%' or ('Transport for London (TfL) is in talks with its American, Australian and European partners about issuing a single contactless card for Paris, New York') like '%New York%';

But doing it this way means that you could possibly bring back more results that you are expecting because it doesn't matter where it is in that string; even if is part of another word. You should be Ok if you are passing full country names through but passing something like united could being back United Kingdom and United States.

webnoob
A: 

you can to try to use php to first query for the list of names you have in the database and then compare it with the text.

 // connect to the db first..

 // select the names from the db
 $sql = "SELECT names FROM countries";
 $result = mysql_query($sql) or die(mysql_error());

 // store names in an array
 $names = array();
 while($data = mysql_fetch_assoc($result)) $names[] .= $data;

then you can use PHP's array functions to extract the names from the text

 // store the text in an array per word
 $exploded_text = explode(' ',$text);

 // get the common strings (the names) from both arrays
 $extracted_names = array_intersect($exploded_text,$names);

 print_r($extracted_names);

i guess there are better and more efficient solutions to this but i hope you get the idea.


IN is used as a shortcut for multiple ORs, so that the following WHERE statement

WHERE (name = 'London' OR name = 'Paris' OR name = 'New York')

becomes

WHERE name IN ('London','Paris','New York')
user398651