FullText Index first Match position - sql-server-2008

I want to do FulltextSearch columns in a table to find CompanyInfo that use keyword "Internet Of things" and at what position in Company Description is this word found.
Company table has ID, CompanyName, briefdescription_company. I have built FullText Index on this column with default stoplist.
I want to do this
CONTAINSTABLE(dbo.Company, briefdescription_company,'ISABOUT("Internet Of things" weight(.1))',LANGUAGE N'English',1000000) as fti
and find the position of the first match only.
How can i do this?
Thanks
Rashmi

Related

mysql select query (dictionary search)

I have a table and I want to get the row with the value that most closely resembles the searched text. For example if searched text is 'rakul' it should be able to select row with value 'rahul'
Just change the Customers value to your table name and the FirstName to what column you want to search and the David to your word want to search for it
SELECT * FROM Customers WHERE FirstName LIKE '%David%';

MySQL Search A Comma Delimited String [duplicate]

Hello i have a string that is stored in my database separated by comma
eg: (new south wales,Queensland,etc,etc)
Know my problem is when i try to search Queensland i am not able to get the result but when i try to search for new south wales i get the record.
But i want to get the result when i try to search for queen or etc.
I am new to php so please help...
Short Term Solution
Use the FIND_IN_SET function:
WHERE FIND_IN_SET('Queensland', csv_column)
...because using LIKE with wildcards on either end is risky, depending on how much/little matches (and it also ensures a table scan). Performance of LIKE with wildcards on either side is on par with REGEXP--that means bad.
Long Term Solution
Don't store comma separated values -- use a proper many-to-many relationship, involving three tables:
Things
thing_id (primary key)
Australian States
State_id (primary key)
State_name
Things_to_Auz_States
thing_id (primary key, foreign key to THINGS table)
State_id (primary key, foreign key to AUSTRALIAN_STATES table)
You'll need JOINs to get data out of the three tables, but if you want to know things like how many are associated to a particular state, or two particular states, it's the proper model.
Not really what you were asking, but just to be complete: you're going to have a lot of trouble unless you change your approach.
The correct way:
TableOne
--------
ThingID
TableTwo
--------
ThingID
Province
Then your database query becomes:
SELECT fields FROM TableOne WHERE ThingID IN
(SELECT ThingID from TableTwo WHERE Province = 'Queensland')
And what do you want to have happen when they search for "Australia"? Get back both Western Australia and South Australia?
By using REGEXP
$result = mysql_query("SELECT * FROM table WHERE column REGEXP $your_search_string");

How to select a column in SQL relative to another one

I have this data base structure:
The field id is the primary key. I am trying to find all courses that have department='civil' & level=4.
#kousik mandal 's answer is correct. But let me give a short explaination to how it works. (since you obviously didn't look online first)
SELECT course_name -- This is the column that you will recieve. if you pick * instead of course_name it will select all columns which match the criteria.
FROM table_name -- you select the course_name from the table 'table_name'
WHERE department='civil' -- This will select columns where the department column has 'civil' as a value
AND level=4 -- This will also check if the level equals to 4.
This might not be as helpful since the answer is already given. but I hope it'll give you some useful information for learning SQL
Try this,
select course_name from table_name where department='civil' and level=4

Matching phrases within delimiters in MySQL

I am looking to perform an exact match on a phrase within specified delimiters in MySQL. I have the following data in a full text index field.
,garden furniture,patio heaters,best offers,best deals,
I am performing the following query which is returning the aforementioned record.
SELECT id, tags
FROM Store
WHERE MATCH(tags) AGAINST(',garden,' IN BOOLEAN MODE)
I only want to return records which contain the value: ,garden, not ,garden furniture, or ,country garden, etc.
It is currently performing a greedy match and ignoring the comma delimiters specified in the query. I have attempted to escape the commas to force them to be included in the query, but this does not work.
Is is possible to specify non-alphanumeric delimiters as part of the match? I want to be able to perform an exact match, like a regular expression i.e '/,garden,/'.
From the docs:
Modify a character set file: This requires no recompilation. The true_word_char() macro uses a “character type” table to distinguish letters and numbers from other characters. . You can edit the contents of the <ctype><map> array in one of the character set XML files to specify that ',' is a “letter.” Then use the given character set for your FULLTEXT indexes. For information about the <ctype><map> array format, see Section 9.3.1, “Character Definition Arrays”.
An other option is to add a new collation.
Either way, you'll have to rebuild the index:
REPAIR TABLE Store QUICK;
Only match against can use an index on your search.
However if your table if not too big, you can use:
SELECT id, tags
FROM Store
WHERE tags LIKE "garden" OR tags LIKE "garden,%" OR tags LIKE "%, garden,%"
There are other options (find_in_set), but I really don't want to go into those, because they perform even worse than the above SQL.
The real problem, never use CSV in a database!
Use CSV in a database is a really really bad idea, because
• It is wasteful, your data is not normalized
• You cannot join on a CSV field
• You cannot use indexes on a CSV field
• Full-text indexes does not play nice with separators (as you've seen)
The answer to create 2 extra tables.
Table tag (innoDB)
----------
id integer primary key auto_increment
tag varchar(50) //one tag per row!
Table tag_link (innoDB)
--------------
store_id integer foreign key references store(id)
tag_id integer foreign key references tag(id)
primary key = (store_id + tag_id) //composite PK
Now you can easily do all sorts of queries on tags.
SELECT s.id, GROUP_CONCAT(t2.tag) FROM store s
INNER JOIN tag_link tl1 ON (s.id = tl1.store_id)
INNER JOIN tag t1 ON (t1.id = tl1.tag_id)
INNER JOIN tag_link tl2 ON (s.id = tl2.store_id)
INNER JOIN tag t2 ON (t2.id = tl2.tag_id)
WHERE t1.tag = 'garden'
GROUP BY s.id
This will select one tag named garden (using t1 and tl1), find all stores linked to that tag and then get all tags linked to those stores (using t2 and tl2).
Very fast and very flexible.

Problem with Mysql index when using "OR" statement

Is there any way how to create an functioning index for this query and to get rid of "filesort"?
SELECT id, title FROM recipes use index (topcat) where
(topcat='$cid' or topcat2='$cid' or topcat3='$cid')
and approved='1' ORDER BY id DESC limit 0,10;
I created index "topcat" ( columns: topcat1+topcat2+topcat3+approved+id) but still ge "Using where; Using filesort".
I can create one more column, lets say, "all_topcats" to store topcat numbers in an array - 1,5,7 and then to run query "... where $cid iIN ()...". But the probem is that in this case "all_topcats" column will be "varchar" but "approved" and "id" columns - int, and index will not be used anyway.
Any ideas? Thanks.
You might improve performance for that query if you reordered the columns in the index:
approved, topcat1, topcat2, topcat3, id
It would be useful to know what the table looks like and why you have three columns named like that. It might be easier to organise a good query if you had a subsidiary table to store the topcat values, with a link back to the main table, but without knowing why you have it set up like that it's hard to know whether that would be sensible.
Can you post the CREATE TABLE?
Edit in response to user message
Your table doesn't sound like it's well-designed. The following design would be better: Add two new tables, Category and Category_Recipe (a cross-referencing table). Category will contain a list of your categories and Category_Recipe will contain two columns, one a foreign key to Category and one a foreign key to the existing Recipe table. A row of Category_Recipe is a statement "this recipe is in this category". You will then be able to very simply write a query that will search for recipes in a given category. You also have the ability to put a recipe in arbitrarily many categories, rather than being limited to 3. Look up "database normalisation" and "foreign keys".