How to search "tags" in MySQL? - mysql

If I have a table on my DB called product_tags with 2 fields: tag_id and tag_name
Here is the schema:
CREATE TABLE `product_tags` (
`tag_id` int(11) NOT NULL auto_increment,
`tag_name` varchar(255) NOT NULL,
PRIMARY KEY (`tag_id`),
UNIQUE KEY `tag_name` (`tag_name`)
) ENGINE=MyISAM AUTO_INCREMENT=84 DEFAULT CHARSET=utf8
Say here some tags in it:
yellow gold
yellow diamond
white gold
rose gold
band
diamond
blue diamond
pink diamond
black diamond
And I want to do a search on the string "yellow gold diamond band"
I only want to pull the following tags:
yellow gold
band
diamond
Because only those tags are exactly in the string. yellow and diamond are both in the string but not together so the yellow diamond tag should be ignored.
-Additionally if possible
If I did the search for "yellow gold blue diamond band"
I only want to pull the following tags:
yellow gold
band
blue diamond
the diamond tag would be ignored because the blue diamond tag would be the match.
How can I do this?

edit:
select
*
from
product_tags P
where
INSTR('yellow gold diamond band', P.tag_name) > 0

Intuitively you could build an algorithm that iterates over all of the possible word combinations formed by contiguous words within the search phrase, and then find which of those is in your tag table. For instance:
yellow gold blue diamond band
Your possible combinations of contiguous would be:
yellow
gold
blue
diamond
band
yellow gold
gold blue
blue diamond
diamond band
yellow gold blue
gold blue diamond
blue diamond band
yellow gold blue diamond
gold blue diamond band
yellow gold blue diamond band
From this entire list, the only terms that match your original list are:
diamond
yellow gold
blue diamond
band
from this list you could cull any items that repeat the same word, favoring the longer option over the shorter with the assumption that the longer option is more descriptive. Thus, after removing those terms you have:
yellow gold
blue diamond
band
This looks like the list you want. Now, this approach works but it will become painfully sluggish as the number of terms in a search phrase increases. For instance, just your 5 terms generated 15 potential tag searches. Imagine if you put in 10 words...
Therefore, my honest recommendation is that you use some sort of punctuation to separate tags within a search, thus making it easier to find tags by simply splitting the searh phrase by the punctuation and searching on those terms, like thus:
yellow gold, blue diamond, band
With a comma-delimited list, you now only have 3 search terms rather than 15, making it much easier to search your table of tags.

Try this:
FROM product_tags
WHERE `tag_name` REGEXP ? LIMIT

You could probably do something like:
WHERE #searchTerm LIKE CONCAT('%', tag_name, '%')
Not very efficient for lots of tags, but it would work in the simple cases given.

I cant think of any good way to do this in SQL directly.
However if i were to implement it in my application logic, this is what the pseudo logic would probably be like
1. Split the search string "yellow gold diamond band" using " " character. string[] search
2. Take the 1st value from the array i.e. yellow in this case.
3. Do a SELECT * FROM product_tags WHERE tag_name LIKE 'yellow%'
4. This will return "yellow gold" and "yellow diamond"
5. Loop through each of the results in 4
a. Split each of these results using " " string [] result
b. If the split array contains has count = 1, we found an exact match for "yellow". No need to search further
c. If the length of the array > 1, Match the search[1] with result[1] till either you have exhausted the split array and find a match or dont find one
d. If more than one match has been found, the longest match is considered
6. Go back to step 2 and repeat for the next string i.e search[1]

Related

How to vary background color based on value

I'm listing out a table that has the results of a tournament. I have the final placements, as well as the original seeds for each team in the tournament. I'm adding a small bubble with a +/- of how the final placement compares to the original seeding. I'd like to make the background get a darker red as the team did worse, and darker green as the team did better.
Example:
Team A finished 1st and was seeded 3rd -> light green
Team B finish 2nd and was seeded 30th -> dark green
Team C finished 10th was seeded 8th -> light red
Team D finish 30th and was seeded 1st -> dark red
I'd like to set a limit obviously for how dark/light the colors get, so teams that out/under performed past a certain amount would all have the same dark tone.
My site is built using Angular, but is this possible to do with just some simple CSS?
Here is a picture from another site that has this already implemented:
As you can see, the final finish is listed on the left, and how well they performed in comparison to their original seeding is on the right. Blank signifies Seed == Finish
You can create an array for the colors
TS:
colors = [{ finished: "1st", color: "light green" }, { finished: "30th", color: "dark green"}]
Loop through your data in a table (or how you prefer) and add ngStyle:
<table>
<th>Team</th>
<th>Finished</th>
<tr *ngFor="let res of colors" [ngStyle]="{'background':getColor(res.finished)}">
<td>res.whateveryourfieldiscalled</td>
<td>res.whateveryourfieldiscalled</td>
</tr>
</table>
And then add it with a ts getColor method:
getColor(finished) {
return this.colors.filter(item => item.finished === finished)[0].color
}

Output based on maximum keywords found

i have list of keyword , i want to search them against title column in my database and return one with maximum keywords :
I thought of using like but that will make query too large based on totle keywords
test case
I have list of keywords like :
Red,bul,win,bot,white
I have table with column title, i want to find title which has maximum number of keywords
Suppose title in my table to be like :
A bottle of red wine
I have a red Bull
The winner bottle of red Bull
Bottle of red wine
i want output to be below one having maximum number of keywords
A White bottle of red Bull
Use this method if you are using the PHP
Add Your keywords in array like below
$Arrkeywords = array('red','bul','win','bot','white');
if(count($Arrkeywords)>0) {
$keywordcondn="";
foreach($Arrkeywords as $keywords){
$keywordcondn.= " and title like '%".$keywords."%'";
}
}
$query= "SELECT * FROM `tbl_title` where title <>'' $keywordcondn";

space is not showing in html

I don't know whether it is a basic question, but I need to ask.
Here is my problem. when a wrote a simple HTML code for showing a paragraph,
but its not getting in the correct format that i wish.
I am adding code below.
<p>
Eosinophil count - absolute
An absolute eosinophil count is a blood test that measures the number of white blood cells called eosinophils.
Eosinophils become active when you have certain allergic diseases, infections, and other medical conditions.
How the Test is Performed
Most of the time blood is drawn from a vein on the inside of the elbow or the back of the hand. The site is cleaned with an antiseptic.
The health care provider wraps an elastic band around your upper arm to make the vein swell with blood.
Next, the provider gently inserts a needle into the vein. The blood collects into an airtight tube attached to the needle.
The elastic band is removed from your arm. The needle is then removed and the site is covered to stop bleeding.
Things to Know
In infants or young children, a sharp tool called a lancet may be used to prick the skin.
The blood collects in a small glass tube, or onto a slide or test strip. A bandage is put on the spot to stop bleeding.
Reference Range
The serum reference ranges of eosinophils are as follows:
Eosinophils blood (%): 0.0-6.0 (This range may vary slightly in different laboratories.)
Eosinophil blood count (absolute): 30-350. The percentage of eosinophils is multiplied by the white blood cell count to
give the absolute eosinophil count. This range may vary slightly in different laboratories.
ANTIGEN SUSPENSION SERUM DILUTION
. 1:30 1:60 1:120 1:240
--------------------------------------------------------------------------------
Salmonella Typhi "O" : -- -- -- --
Salmonella Typhi "H" : -- -- -- --
Salmonella Para Typhi "AH" : -- -- -- --
Salmonella Para Typhi "BH" : -- -- -- --
</p>
But in output all the spaces are truncated.
How to get the exact text as output?
Check white-space: pre; property in css here
p {
white-space: pre;
}
<p>
Eosinophil count - absolute
An absolute eosinophil count is a blood test that measures the number of white blood cells called eosinophils.
Eosinophils become active when you have certain allergic diseases, infections, and other medical conditions.
How the Test is Performed
Most of the time blood is drawn from a vein on the inside of the elbow or the back of the hand. The site is cleaned with an antiseptic.
The health care provider wraps an elastic band around your upper arm to make the vein swell with blood.
Next, the provider gently inserts a needle into the vein. The blood collects into an airtight tube attached to the needle.
The elastic band is removed from your arm. The needle is then removed and the site is covered to stop bleeding.
Things to Know
In infants or young children, a sharp tool called a lancet may be used to prick the skin.
The blood collects in a small glass tube, or onto a slide or test strip. A bandage is put on the spot to stop bleeding.
Reference Range
The serum reference ranges of eosinophils are as follows:
Eosinophils blood (%): 0.0-6.0 (This range may vary slightly in different laboratories.)
Eosinophil blood count (absolute): 30-350. The percentage of eosinophils is multiplied by the white blood cell count to
give the absolute eosinophil count. This range may vary slightly in different laboratories.
ANTIGEN SUSPENSION SERUM DILUTION
. 1:30 1:60 1:120 1:240
--------------------------------------------------------------------------------
Salmonella Typhi "O" : -- -- -- --
Salmonella Typhi "H" : -- -- -- --
Salmonella Para Typhi "AH" : -- -- -- --
Salmonella Para Typhi "BH" : -- -- -- --
</p>
use &nbsp for space in HTML
In HTML, multiple spaces are automatically replaced by only one space except if your text is surrounded with <pre> tag.
If you want your spaces in another type of tag (e.g. <p>) you need to use HTML entities. The HTML entity for a normal space is and for a tab character it's &tab;. There's a lot of HTML entities like those. Here is a good reference to find the HTML entities you need: HTML entities reference chart

Finding the best results using Tags

I want to create a site that uses tags to find a best matching result (a post). A user can like/dislike things and in turn builds a "profile" for himself that consists of tags he likes (and tags he dislikes) based on the things he likes/dislikes.
For example:
USER_1 likes: dog, mouse, blue, house
USER_1 dislikes: cat, pink
USER_2 likes: red, hat, blue
USER_2 dislikes: pink
...
Now for the posts I have similar tags in the database. For example:
POST_1 tags: dog, red, hat
POST_2 tags: blue, pink, cat
...
My question is:
How can I get the best matching result (based on likes alone | including dislikes)? Is there a system that has been established or how does one usually solve problems like these?
E.g. USER_2 first receives POST_2 because it matches his like "blue" and "hat", after that comes POST_1 because he likes "blue".
Thanks! I'm eager to learn how this works but I haven't found anything (and don't know where to start).

Matching /comparing a Database Field using a Key with different **combination** in Mysql 5.0

I have a table: db_details in MYSQL 5.0 with a Table field: Description having 10,0000 entries . I want an optimum method to search a key and get the result with different combination.For example:
Let the 4 entries of description [actually its 10000] field is as follows:
description 1 : I want to see my friend
description 2: My friend has a red pen and yellow box
description 3: This yellow box belongs to my friend
description 4: He is a doctor
I want to compare these with a key : pen box friend and should get result in the order: first description 2, then description 3 and then description 1 [Its because description 2 has the max hit ,then description 3 and so on]
please help me. I want to know how it should be done..Since it is a Huge database ,i want to get this without a delay whether by using page offset or not.
If you want to do this, i think Shpinx is more suitable for do this job.
Sphinx is much faster and you can set priority of results.
STEP 1 : create table description(descr TEXT,fulltext(descr));
STEP 2 : insert into description values('I want to see my friend'),('My friend has a red pen and yellow box'),('This yellow box belongs to my friend'),('He is a doctor');
STEP 3 : select * from description where match(descr) against('pen box friend');
Output is as follows : (as you required in order)
My friend has a red pen and yellow box
This yellow box belongs to my friend
I want to see my friend
Note: But it will be comparatively slow as compared to sphinx.