I'm struggling to write a SQL statement - Im basically trying to distinguish between postcodes by checking if there is a number after the first letter.
For example i could have records with BL1 as postcode or B12, and when i enter in the search 'B' i only want it to select the records that have a numerical value after it - then if i search BL obviously that would just be a 'BL%' select.
With it being var char as well i'm struggling with this one, also its MySql.
Is it possible to convert then check the second char then condition it in mySql?
Thanks
You can use a Regular Expression
SELECT *
FROM tableName
WHERE col REGEXP '^B[0-9]';
This matches only those values which start with B and have a digit next to it. If you want to expand the regex to also include alpha characters on second position you could do
WHERE col REGEXP '^B[0-9A-Za-z]';
Fiddle
Related
I have a query,
e.g.
name column have "Rodrigue Dattatray Desilva".
I want to write a query in such a way that,
If I search for 'gtl' and match anywhere in string it should show the result.
I know in PHP I can apply the patch like '%g%t%l%'.
But I want to know MySql way.
Note: I can search for anything, I am just giving above an example.
EDIT:
create table Test(id integer, title varchar(100));
insert into Test(id, title) values(1, "Rodrigue Dattatray Desilva");
select * from Test where title like '%g%t%l%';
Consider the above case. Where "gtl" is string I am trying to search in the title but search string can be anything.
gtl is string where it exists in the current title but not in sequence.
The easy answer is that you need an extra wildcard:
select * from Test where title like '%g%t%l%';
The query you posted does not have a wild card after the 'l', so would only match if the phrase ended with 'l'.
The more complicated answer is that you can also use regular expressions, which give you more power over the search.
The even more complicated answer is that performance of these string matching queries tends to be poor - the wild cards mean that indexes are usually ineffective. If you have a large number of rows in your table, full-text searching is much faster.
You can do the same in Mysql too.
You can use the keyword like in MySql.
% - The percent sign represents zero, one, or multiple characters
_ - The underscore represents a single character
I have prepared a spreadsheet containing several entries, and each row of entry provides two columns of information. One is a product name, and another one is the price of the product. An Example is shown as follows:
Product (Column A) Price (Column B)
1. Watch $100
2. Mousepad $50
3. Notebook $1000
I am trying to create a web-based database with PHP and MYSQL. The basic searching function is that when I enter a sentence in a search box, such as "I have a watch and a notebook", those entries in my database matching some of the words in the sentence would be given (i.e. row 1 "Watch" and row 3 "Notebook"). Does MySQL support this kind of search, and how can I write the query to do so?
Writing an SQL query to do that would not be hard. All you need is to write a WHERE clause that searches for the column value inside your string. MySQL has a LOCATE() function you can use to search for substrings. In your example, you may have:
SELECT *
FROM myTable
WHERE LOCATE(product, 'I have a watch and a notebook.') <> 0;
It is important to note, as you'll see in the link, locate returns 0 if the substring is not found.
Here is an SQL Fiddle example.
I am using fulltext search for search query. when i search two words i want exact two words match results. here problem is i am getting one word match results also. the data contains polish language characters.
My code is
$qry="select * from tbl_jobs where MATCH(job_title) AGAINST('+Młodszy +Konsultant' IN BOOLEAN MODE)";
I am getting results as
1. [job_title] => młodszy konsultant ds. sprzedaży
2. [job_title] => Młodszy Konsultant
3. [job_title] => Konsultant ds. sprzedaży młodszy
4. [job_title] => Telefoniczny konsultant
I have the following indexes
Here first 3 results are correct but 4th result is wrong. the job_title contains only one matching word results. I want two search words mandatory results set.
query is also correct i am using utf8_general. Where is exact problem please help me
If you have two words say abc and xyz to match and you want to match both then you can use wildcard % it will match just like regular expression matching
select * from mytable where job_title like '%abc%xyz%' or job_title like '%abc%xyz%'
It will give you the only records where job title contains both words abc and xyz
For full text searching, you can have a full detailed video here. Plus a tip for you
don't use " * " unless you want to retrieve the whole columns in a
table. Instead use the column qualified name like if there is a table
of students having a roll no then,
SELECT students.roll_no FROM students
Link for the tutorial btw!
I have a list of keywords in a field called 'keywords' in a MYSQL database For example the keywords field in the record contains 3 sample records
KEYWORD LIST 1: tree,apple,banana,cherry,flower,red apple,pink cherry,cat,mouse
KEYWORD LIST 2: cat,mouse,apple,red apple,flower,red appleberry
KEYWORD LIST 3: apple, red appleberry, flower
The keyword list is NOT an array - just a text field in a mysql database that has a lot of other fields.
When I run a MYSQL SELECT query from what I've seen 'so far' there are two ways: a) like %% b) match against
Let's say I want to run a query against the word 'apple'
SELECT * where keywords like '%apple%' from table
This would bring up the records that contain the word 'apple' including the record above but it wouldn't necessarily give me any sequence of higher or lower. I'd have to do do the filtering of the results post query.
Supposing I was more detailed in my query, and chose 'red apple' it would still show a match, but I wouldn't necessarily get have KEYWORD LIST 1 be more relevant than 2 or 3.
Someone suggested using the Match Against instead
SELECT *, MATCH(Keywords) AGAINST('apple') AS Score
FROM table
WHERE Keywords like '%red apple%'
ORDER BY score DESC;
This is certainly heading in the right direction - however will it won't sort the results by an exact match being found as more relevant. The relevancy would be based on how many repetitions of the word 'apple' appeared in the keyword list (this is the age old reason why all the search engines chose to ignore keywords altogether) - do you see where I'm going with this?
What I'm looking for is do process most of the logic in the MYSQL instead of reading it into an array and processing it in PHP as others have suggested. Hence this simplistic example.
Here's what the query ought to how it should be:
1) if my query is 'apple' Keyword list 1 should show up first
2) if my query is 'red apple' then Keyword list 2 should show up First because the word 'red apple' is CLOSER to the FRONT of the keyword list - closer to the beginning of the string.
BUT because of the LIKE %%
Keyword List 3 would show up even though the match was 'red appleberry' instead of 'red apple'
(It would be simplest if MySQL had some kind of 'explode' function that you could specify the comma (',') as the delimiter in the match but I don't know of any such method, do you? Of course you'd have to read the entire list of results into an array and then explode them in PHP.
3) Suppose I search for 'red apple' and here's the bug: I'd still have a match on 'red appleberry' (record 3) - I don't want that. Record 2 should show up and then Record 1 and not even show record 3.
This is a discussion and an inquiry. Anyone have any suggestions?
My OWN ANSWER:
include the comma in the query
instead of searching for 'red apple' search for 'red apple,'
but what if the user put a space between - or if its at the tail end of the list of keywords?
QUESTION:
How can we search for specific SCORE in a comma delimited text field in MYSQL that uses the exact word (not a fragment) to come up with an ORDERED list of results.
Every attempt on this so far will still have Keyword List 3 show up higher than 2 even though red appleberry and red apple are two different phrases separated by commas.
Thanks! Let's discuss!
For small project, you can do something like this
SELECT *,
case when keyword like '%red apple%' then 1 else 0 end exact_match,
MATCH(Keywords) AGAINST('apple') AS Score
FROM table
where keywords like '%apple%'
ORDER BY exact_match DESC, score DESC;
PDO, I want to use a search query. I tried to use like query, using % on both front and end of search variable. but it search in between the words.
like if in database there is name Jhon Anderson and if i use search variable using LIKE %erson%, The query will get me the row which contains the above name.
But what i want is query should only search with starting alphabets so i tried the LIKE query with Jhon%, i got the desired result, but using this % in the end only works if search variable matches the starting of first word, but what about the word after the space?
I mean if i try Anderson% or Ander%, The query will not get me the result of this row as Anderson is the second word in the field Jhon Anderson after space.
So my question is how to search in the words after spaces, that if there starting alphabets match the search query.
I don't know if such kind of query exist or not, but i think it might be possible with some looping after using like query with wildcard '%'.$variable.'%' and after getting all the results unset the arrays or rows where search variable do not match with the first alphabet of the result.
Any ideas how to implement such kind of search.
Query right now i am using,
$stmt = $conn->prepare("SELECT ROOM, GUEST_NAME, GUEST_FIRST_NAME, CONFIRMATION_NO, DEPARTURE, PWD FROM RESERVATION_GENERAL_2 WHERE LOWER(GUEST_FIRST_NAME) LIKE ? OR LOWER(GUEST_NAME) LIKE ?");
$stmt->execute(array('%'.strtolower($searchFilter).'%','%'.strtolower($searchFilter).'%' ));
A bit dirty, but simple solution is to include the space in the search:
WHERE ' ' || Name LIKE '% Anderson%'
You see I added a space in front of Name, so it will also find the word if it is at the beginning of the string.
Alternatively, you can use REGEXP_LIKE, but then it is still a bit awkward, since Oracle doesn't have a word boundary expression in its regex engine. For a solution, see this answer