i'm making a query in mysql but i have a problem: this is my column structure
|country|
--------
Boston
--------
Chicago
--------
washington
The thing is i may have a search item like:
North Washington
Boston Easht
South Chicago
So i'm trying to match it using the %like% operador like that:
select * from am_ciudad where Ciu_Nombre LIKE '%NORTH BOSTON';
select * from am_ciudad where Ciu_Nombre LIKE 'CHICAGO%';
select * from am_ciudad where Ciu_Nombre LIKE '%South Chicago%';
the second one makes match because it starts with "chicago" word, but in the case of the query has a prefix it doesn't, is there a way to search by at least one match in the query string?
IN method
Use comma separated list of your search query:
SELECT * FROM am_ciudad WHERE Ciu_Nombre IN('North', 'Washington', ...)
REGEXP method
I can imagine the REGEXP will be slower, but I haven't benchmarked it.
SELECT * FROM am_ciudad WHERE Ciu_Nombre REGEXP(North|Washington|...)
Your other searches won't match because they do not exist.
If you want to match Boston in the phrase I love Boston Red Sox then you would need to use ...LIKE '%Boston%'; the %s are wild cards so using them before and after the word you are tying to match tell the query that you don't care what come before and after. Your search string of ...LIKE '%NORTH BOSTON'; is telling query that you are looking for <anything>North BOSTON; which obviously you don't have.
Hopefully that makes sense and helps out.
S
Im not sure if your version of mysql supports it but its worth trying.
select * from am_ciudad where Ciu_Nombre in (NORTH,BOSTON);
Same for the others, just replace the space with ','
Related
I have a mySQL table that contains restaurant information. Part of that information is a comma separated list of numbers that corresponds to the type of cuisine the restaurant serves. I'm having some problems getting the correct information out of the database. Table looks like
id businessName cuisine_id
1 Pizza Place 2,3,4,
2 Burger Place 12,13,14,
I came up with
SELECT * FROM restaurant WHERE cuisine_id LIKE "%2,%"
But that leaves me with the problem that it matches "2," "12," and 22,".
I also tried
SELECT * FROM restaurant WHERE cuisine_id LIKE "[^0-9]2,%"
But that returned nothing.
Any advice on how to write this expression?
Use regexp
SELECT * FROM restaurant WHERE cuisine_id REGEXP "(^|,)2,"
For num exact match,
SELECT * FROM restaurant WHERE cuisine_id regexp "(^|,)2(,|$)"
Note that ^, $ are mentioned as regex anchors which matches the start and end end of a line. This (^|,) will match either a start of a line or comma. So this ensured that the following pattern must be at the start or preceeded by comma.
There's no need to use regular expressions, you could use FIND_IN_SET string function:
SELECT * FROM restaurant WHERE FIND_IN_SET('2', cuisine_id)>0
or use CONCAT:
SELECT * FROM restaurant WHERE CONCAT(',', cuisine_id, ',') LIKE '%,2,%'
or better to normalize your database structure (is often not a good idea to store comma separated values in a single field)
Is it possible in MySQL to select rows for a certain range of items?
For example when I want to select all items in where the first letter of the NAME is between the B and T, alphabetically.
I know I can make this is PHP aswell, but it would save me a bit of time if this is possible in MySQL...
Is it possible, and if so, how?
The ideal situation would be something like this:
$sql="SELECT * FROM paths FROM name=name1 TO name=name6"; //which would select name1, 2, 3, 4, 5, 6.
Using BETWEEN will basically get you there, but you need to use one letter past where you want to end. Experiment until you get the result you desire.
SELECT * FROM paths WHERE UPPER(name) BETWEEN 'B' AND 'U';
The idea here is that everything beginning with a 'T' will sort alphabetically before anything beginning with a 'U'. You need to convert it to upper-case via UPPER() so you don't run up against potential collation problems.
So your results could be like:
B,
Bill
Bob
Jane
Tommy
Travis
But Uwe (He's German) would be excluded.
You can use BETWEEN like:
SELECT * FROM paths WHERE name BETWEEN 'B' AND 'U'
I have a table call objects which there are the columns:
object_id,
name_english(vchar),
name_japanese(vchar),
name_french(vchar),
object_description
for each object.
When a user perform a search, they may enter either english, japanese or french... and my sql statement is:
SELECT
o.object_id,
o.name_english,
o.name_japanese,
o.name_french,
o.object_description
FROM
objects AS o
WHERE
o.name_english LIKE CONCAT('%',:search,'%') OR
o.name_japanese LIKE CONCAT('%',:search,'%') OR
o.name_french LIKE CONCAT('%',:search,'%')
ORDER BY
o.name_english, o.name_japanese, o.name_french ASC
And some of the entries are like:
Tin spoon,
Tin Foil,
Doctor Martin Shoes,
Martini glass,
Cutting board,
Ting Soda.
So, when the user search the word "Tin" it will return all results of these, but instead I just want to return the results which specific include the term "Tin" or displaying the result and rank them by relevance order. How can I achieve that?
Thanks.
You can use MySQL FULLTEXT indices to do that. This requires the MyISAM table type, an index on (name_english, name_japanese, name_french, object_description) or whatever fields you want to search on, and the appropriate use of the MATCH ... AGAINST operator on exactly that set of columns.
See the manual at http://dev.mysql.com/doc/refman/5.5/en/fulltext-search.html, and the examples on the following page http://dev.mysql.com/doc/refman/5.5/en/fulltext-natural-language.html
After running the query above , you will get all sort of results including ones that you are not interested, but you can then use regular expressions on the above results(returned by mysql server) set to filter out what u need.
This should do the trick - you may have to filter out duplicates, but the basic idea is obvious.
SELECT
`object`.`object_id`,
`object`.`name_english`,
`object`.`name_japanese`,
`object`.`name_french`,
`object`.`object_info`, 1 as ranking
FROM `objects` AS `object`
WHERE `object`.`name_english` LIKE CONCAT(:search,'%') OR `object`.`name_japanese` LIKE CONCAT(:search,'%') OR `object`.`name_french` LIKE CONCAT(:search,'%')
union
SELECT
`object`.`object_id`,
`object`.`name_english`,
`object`.`name_japanese`,
`object`.`name_french`,
`object`.`object_info`, 10 as ranking
FROM `objects` AS `object`
WHERE `object`.`name_english` LIKE CONCAT('%',:search,'%') OR `object`.`name_japanese` LIKE CONCAT('%',:search,'%') OR `object`.`name_french` LIKE CONCAT('%',:search,'%')
ORDER BY ranking, `object`.`name_english`, `object`.`name_japanese`, `object`.`name_french` ASC
I want to find the data from table artists where name is start with letter a, b, c.
i.e.
My o/p should contain
Adam
Alan
Bob
Ben
Chris
Cameron
But not GlAin, doC, dolBie
In MySQL use the '^' to identify you want to check the first char of the string then define the array [] of letters you want to check for.
Try This
SELECT * FROM artists WHERE name REGEXP '^[abc]'
You can use like 'A%' expression, but if you want this query to run fast for large tables I'd recommend you to put number of first button into separate field with tiny int type.
Try this:
select * from artists where name like "A%" or name like "B%" or name like "C%"
One can also use RLIKE as below
SELECT * FROM artists WHERE name RLIKE '^[abc]';
Try this simple select:
select *
from artists
where name like "a%"
I would go for substr() functionality in MySql.
Basically, this function takes account of three parameters i.e.
substr(str,pos,len)
http://www.w3resource.com/mysql/string-functions/mysql-substr-function.php
SELECT * FROM artists
WHERE lower(substr(name,1,1)) in ('a','b','c');
try using CHARLIST as shown below:
select distinct name from artists where name RLIKE '^[abc]';
use distinct only if you want distinct values only.
To read about it Click here.
I have a list of movies that I have grouped by letter. Naturally, the movies starting with the letter "T" have about 80% of movies that begin with "The". Movies such as "The Dark Knight" should appear in the "D" list, and preferably in the "T" as well. Any way I can do that?
I use the following code in the WHERE clause to display movies that start with a certain letter, ignoring "the", but this also had a convenient side effect of having a movie such as "The Dark Knight" appear for letter "D" and "T".
WHERE movie_title REGEXP CONCAT('^(the )?', '$letter')
I would like to achieve this when I echo out all the movies that are in the database.
If you are going to be performing this query frequently, you will want to create a separate field in the table with the 'sorted' name. Using regular expressions or other operations make it impossible for MySQL to take advantage of the index.
So, the simplest and most efficient solution is to make your add a movie_title_short field, which contains movie_title without the "The" or "A". Be sure to add an index to the movie_title_short field too!
As Carl said, I'd build this into its own indexable field to avoid having to compute it each time. I'd recommend doing it in a slightly different way to avoid redundancy though.
movies (id, name, namePrefix)
eg:
| Dark Knight | The |
| Affair To Remember | An |
| Beautiful Mind | A |
This way you can show these movies in two different ways: "name, namePrefix" or "namePrefix name" and can be sorted accordingly.
select right(movie_title, char_length(movie_title)-4) as movie_title
from movies
where left(movie_title,3) = 'the'
union
select movie_title
from movies
You can use the mysql replace function in the select clause...
select replace(movie_title,'The ','') from ... order by replace(movie_title,'The ','')'
Just had that problem myself... solution is:
SELECT * FROM movies WHERE title REGEXP '^d' AND title NOT REGEXP '^the ' OR title REGEXP '^the d'
this will give you only results that starts with "The D" or "D"
Use this:
SELECT * FROM movies ORDER BY TRIM(LEADING 'the ' FROM LOWER(`movie_title`));