Query string with spaces - mysql

Is is possible to query string with spaces, a sentence for example, I have following query:
select id_group from groups where title = 'some title with spaces'
And I don't get any results although I have exact title in my database. I also tried with like but it's the same

Try this to get all titles with a white space.
select id_group from groups where title like '% %';
But you may find performance issues if there are many number of records and if the column is not indexed.

Yes, its possible, maybe try with double quotes? y just tried in my database
SELECT *
FROM `Faq`
WHERE faq_pregunta = "chao que te vaya bien"
and it works

There's nothing wrong with your query. The only explanation is that there just isn't any row that satisfies the condition.

It is possible. if you are using phpmyadmin you can press on "sql" and then try your code directly in the database. The problem i think is that you may use lack of ' or " or some other type of symbol. The problem can also be that you havent typed the exact same line. It is important to use capital on the same places and so on.

Related

MySQL how to get a specific row from a table

I have a database with the fields: land, zone, domestic. And I want to get all this info out depending on what land the user chooses. So I figured I do this:
SELECT * FROM myDB WHERE land=Norway (Norway is a stored country).
But I get the error unknown column Norway. Is this because it cant find Norway or what seems to be the problem?
It looks like you aren't specifying a table name try something like this:
SELECT * FROM MyDB.MyTable
WHERE land = 'Norway'
Also note that string Norway is in single quotes
You missed the single quotes before and after the country name that you've used, as per my comment on the question earlier!
SELECT * FROM myDB WHERE land = 'Norway'
The above query works on the assumption that your table is actually named as myDB
You need quotes around literal
SELECT * FROM myDB.tablename WHERE land='Norway';
(and a proper tablename) otherwise your value is used like a column name and give your the error Unknown column Norway.
You are query using a where clause that confront a column with text. So to compare you have to tell to the dbms the value to match . Using only Norway it doesn't understand that you are asking for the row with Norway value. Using 'Norway' you are telling "hey dbms here you are your text to confront". I hope that I'm to help.
You can use ` marks for choosing columns/tables, so you should do something like
SELECT `column1`,`column2` FROM `tablename` WHERE `column`="value"

MySql full text search to consider extra s es

I need a full text search query to search a word, also it should consider extra s and es at the end. If I put * at the end it matches with lot of extra characters as well which are not needed.
eg;
select item_description from item_description_mapping where match(item_description)
against('+maintain*' in boolean mode) ;
This query gives results
Maintain
maintainance
Maintains
Here we require only maintain, maintains like that not maintainance.
Is there something like regex combination with boolean mode in full text search or we can use length function with exact match??
Thanks,
Ashish
I got answer for this,
select * from index_table where item_name rlike '[[:<:]]preform[s]*[es]*[ies]*[[:>:]]';
I am posting this here coz it might be helpful for others in need.
Thanks,
Ashish

MySQL search within the last 5 characters in a column?

My user table has a column "name" which contains information like this:
Joe Lee
Angela White
I want to search for either first name or last name efficiently. First name is easy, I can do
SELECT * FROM user WHERE name LIKE "ABC%"
But for last name, if I do
SELECT * FROM user WHERE name LIKE "%ABC"
That would be extremely slow.
So I am thinking about counting the characters of the input, for example, "ABC" has 3 characters, and if I can search only the last three characters in name column, that would be great. So I want something like
SELECT * FROM user WHERE substring(name, end-3, end) LIKE "ABC%"
Is there anything in MySQL that can do this?
Thanks so much!
PS. I cannot do fulltext because our search engine doesn't support that.
The reason that
WHERE name LIKE '%ith'
is a slow way to look for 'John Smith' by last name is the same reason that
WHERE Right(name, InStr(name, ' ' )) LIKE 'smi%'
or any other expression on the column is slow. It defeats the use of the index for quick lookup and leaves the MySQL server doing a full table scan or full index scan.
If you were using Oracle (that is, if you worked for a formerly wealthy employer) you could use function indexes. As it is you have to add some extra columns or some other helping data to accelerate your search.
Your smartest move is to split your first and last names into separate columns. Several other people have pointed out good reasons for doing that.
If you can't do that you could try creating an extra column which contains the name string reversed, and create an index on that column. That column will have, for example, 'John Smith' stored as 'htimS nhoJ'. Then you can search as follows.
WHERE nameReversed LIKE CONCAT(REVERSE('ith'),'%')
This search will use the index and be decently fast. I've had good success with it.
You're close. In MySQL you should be able to use InStr(str, substr) and Right(str, index) to do the following:
SELECT * FROM user WHERE Right(name, InStr(name, " ")) LIKE "ABC%"
InStr(name, " ") returns the index of the Space character (you may have to play with the " " syntax). This index is then used in the Right() function to search for only the last name (basically; problems arise when you have multiple names, multiple spaces etc). LIKE "ABC%" would then search for a last name starting with ABC.
You cannot use a fixed index as names that are more than 3 or less than 3 characters long would not return properly as you suggest.
However, as Zane said, it's a much better practise to use seperate fields.
If it is a MyIsam table, you may use Free text search to do the same.
You can use the REGEXP operator:
SELECT * FROM user WHERE name REGEXP "ABC$"
http://dev.mysql.com/doc/refman/5.1/en/regexp.html

MySQL UNION query correct handling for 3 or more words

I've to ask your help to solve this problem.
My website has a search field, let's say user writes in "Korg X 50"
In my database in table "products" i have a filed "name" that holds "X50" and a field "brand" that hold "Korg". Is there a way to use the UNION option to get the correct record ?
And if the user enters "Korg X-50" ?
Thank you very much !
Matteo
May be you should use full-text search
SELECT brand, name, MATCH (brand,name) AGAINST ('Korg X 50') AS score
FROM products WHERE MATCH (brand,name) AGAINST ('Korg X 50')
As far as I understand you don't need UNION but something like
SELECT * FROM table1
WHERE CONCAT(field1, field2) LIKE '%your_string%'
On client side you get rid of all characters (like space, hyphen, etc) in your_string that appears in user input and cannot be in field1 or field2.
So, user input Korg X 50 as well as Korg X-50 becomes KorgX50.
you will need to get some form of searchable text.
either parse out the input for multiple key words and match each separately, or perhaps try to append them all together and match to the columns appended in the same way.
you will also need either a regex, or maybe a simpler search and replace to get rid of spaces and dashes after the append before the comparison.
in general, allowing users to search for open ended text strings is more complicated than 'what union do i use'... you will ideally also be worried about slight misspellings and capitalization, and keyword order.
you may consider pulling all keywords out from your normal record into a separate keyword list associated with each product, then use that list to perform your searches.
If you do not want to parse user input and use as it is, then you will need to use a query like this
select * from products where concat_ws(' ',brand,name) = user_input -- or
select * from products where concat_ws(' ',brand,name) like %user_input%
However, this query won't return result if user enters name "Korg X-50" and your table contains "Korg" and "X50", then you need to do some other thing to achive this. You may look at http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_soundex however it won't be a complete solution. Look for text indexing libraries for that ex: lucene

MySQL Find similar strings

I have an InnoDB database table of 12 character codes which often need to be entered by a user.
Occasionally, a user will enter the code incorrectly (for example typing a lower case L instead of a 1 etc).
I'm trying to write a query that will find similar codes to the one they have entered but using LIKE '%code%' gives me way too many results, many of which contain only one matching character.
Is there a way to perform a more detailed check?
Edit - Case sensitive not required.
Any advice appreciated.
Thanks.
Have a look at soundex. Commonly misspelled strings have the same soundex code, so you can query for:
where soundex(Code) like soundex(UserInput)
use without wildcard % for that
SELECT `code` FROM table where code LIKE 'user_input'
thi wil also check the space
SELECT 'a' = 'a ', return 1 whereas SELCET 'a' LIKE 'a ' return 0
reference