I currently have a table for products with it's own set of tags and a table for news with it's own set of tags. I wanted to add related news to the products page so I was thinking of using like but since the column tags in the products page is something like
(Products) tags- manutd, man utd, football
(news) tags - manutd, blah, bruha [this one is related]
(news) tags - man, utd, bruha [this one is not related]
I wanted to use a query to show all news containing any of the tags(from products) seperated by commas using mysql. How should I go about constructing such a query? If there is a better way of doing this a little explanation would be helpful too. Thanks
Do you have the product tags at hand or do you want to join the two tables based on their tag similarity? In the first case, I would try something like this:
select ...
from News n
where n.tags REGEXP 'manutd|man utd|football'
Note that I used the product tag string you provided above, replaced the commas by | and removed the whitespace to the left and right of the commas.
Related
How i could remove different links i have in a MySQL field in many rows? I couldn't find a query for that.
I don't have much experience with MySQL, the ideal solutions would be something that begins with something and ends with something.
Example:
On table 'ads' i have a field called 'description' that has the ad description text. I have links inside the descriptions and i want to remove them all, but those links have different urls from ads to ads. I don't want to delete the text of the link, just the link itself <a class="something" href="http://someurl.com">Link text</a>
Please give a better explanation of your problem and a sample of code.
Although, if i'm right on what you want to achieve, you might need something like this below.
Example:
UPDATE ads SET description=REPLACE(description,'whatYouWantToDelete','')
The above will remove everything in your column that contains the string foo.
UPDATE:
These might be helpful.
Remove HTML tags from record
Removing links from posts in wordpress using query
I'm organizing articles in a big database and I face a problem - I need to find all articles with two or more links in them.
Every link is HTML link and has form .... How do I SELECT from article database with all links that a have at least two a href in them?
I was taught how to select one a href but two?...
SELECT * FROM `Articles5` WHERE
content LIKE "%a href%"
How to double this?
Had you tried using your own code but twice?
SELECT * FROM `Articles5` WHERE
content LIKE "%a href%a href%"
At first you can get all Articless WHERE content LIKE "%a href%" and put in temporary table. Then replace this value and find "a href" once more.
P.S For this purpose I advice you to use FullTextSearch
I have an image table with a column called tags. A typical entry would look like this:
id link tags
-------------------
1 [link] funny,not-cool,work
I have a search page where users can enter tags to search for images. They could enter any amount of tags separated by a comma
Search: funny, fall, fail
Question is, what is the best way to search the database for these tags? Would a simple LIKE be it? I know it may have been more ideal to add each tag in a different table and not have them comma separated so if I need to change the way the tags work I will.
It would probably be better do have multiple tables here. The first table would be called images and just id and link. The second table would be called tags with each row being one specific tag such say columns id and tag. The third table would be a join table called something like imagetags with columns id, image, and tag. The image column in imagetags would be a foreign key to images and the tag column would be a foreign key to tags.
Using your example search the SQL would be something like:
SELECT images.link
FROM images
JOIN imagetags on images.id = imagetags.image
JOIN tags on tags.id = imagetags.tag
WHERE tags.tag in ('funny','fall','fail')
Basically I would like to have posts with tags and users with their tags subscription
What shall be my structure? I would like to serve users the posts which contains tags that they subscribed.
Now I would like you to examine two cases
1) There are no tags - but categories - one category per post
2) There are tags - many tags per post
In the second case should I have four tables and do an enormous join between them all ?
Table structure for your need
Contentid Topic Catgoryid Tags
More than one tag can be separated by , and topic with no tag will be null . You can select topic for particular tag by using like in select query
I'm using a song database system to learn php with mysql on a many to many relationship database. Below is displayed how the three tables are organized:
Songs Link Tags
======= ===== =======
Sid Sid Tid
Songname Tid Tagname
Now I was thinking what if there are tag synonyms? As i am calculating a matching percentage for each song combined with the tags eneterred. I don't want to enter 20 tags for 1 song, so i cover all posibilities, but therefore ruining the matching percentage because 4 tags were enterred and this resulting in 4/20 match aka 20% whereas it maybe had to be 4/8 (50%).
Is there a smart way to create a table for synonyms, making sure the tags are 'groupped', so that the enterred tags are first checked with this table and then this/these 'head tag(s)' get matched with the link table resulting in songname and a matching percentage of tags enterred / total amount of 'head tags' × 100%?
And on top of that how would you create a query that when a new song is enterred, new tags are enterred into a group they belong to and existing ones get skipped. While linking this head tag also to the new song?
I'm not sure if this idea is anything realistic, but I wonder how other systems would do this to still make sure you get an accurate match while also taking synonyms into account.
You could add a third row to table Tags:
Tags
--------------
Tid
Tagname
HeadTagId
So every Tag would refer to a HeadTag, and HeadTags would refer to themselves.
Regarding queries, I imagine two scenarios. Either
- tags are predefined: when a new song is entered, you just add one row to Songs and as many to Link as the tags it has. No need to touch table Tags.
or
- new tags can be defined by users: when a new song is entered, check if tags already exist and, if not, the user should be able to enter new tags, and define them either as head tags or choose an existing 'head tag' for them (i.e. a tag referenced in the third row of Tags).
In any case, to retrieve head tags for a given song, you would need something similar to:
SELECT HeadTagId FROM Tags
JOIN Link ON Tags.Tid=Link.Tid
WHERE Link.Sid=12345
and if you want the name of the head_tag directly (instead of the Id):
SELECT Tagname FROM Tags AS TagParents
JOIN Tags AS TagChildren ON TagParents.Tid=TagChildren.HeadTagId
JOIN Link ON Link.Tid=TagChildren.Tid
WHERE Link.Sid=12345
Edit: You don't need a many-to-many relation in this case, because each tag must either declare that it is a main tag or that it is a synonym, and point to the main tag. That is accomplished by the third row. To me it makes sense to include it in the same table, as it is a necessary attribute of each tag, and each tag will only have one such attribute.
So main tags refer to themselves, that is:
Tid: 1
Tagname: 'Classical'
HeadTagId: 1
And synonyms refer to main tags:
Tid: 2
Tagname: 'Classical music'
HeadTagId: 1
The only issue with this approach is that you don't have a straightforward list of main tags, but you could generate it with a simple:
SELECT DISTINCT HeadTagId FROM Tags
sure - part of your answer is just go a little further:
tag_synonym
-----------
Tid
Tid2
then you can query to see if any of the synonyms are linked as well as the original tag