How to get combination of related rows - php

I've a large table of articles, and want to get related articles based on a similarity of some kind - for example i've got 2 articles; The first article's title is "Article Part 1" and the second article is "Article Part 2".
What I want effectively is an output of the most likely matches for an article based on title either if its a variation of the current article or whether a continuation of the article or a modification.
Example:
article_title friendly_url id
Article 1 article-1 013
Article 2 article-2 023
Example 1 example-1 034
Example 3 example-3 016
Example 2 example-2 015
So I would ideally like a kind of string match based on the title so then, the "Article 1" related article(s) would be "Article 2" or "Example 3" related article(s) would be "Example 1" and "Example 2".
Note: The title can be more than 2 words and they may include special characters, for example, a title may be "How to: tutorial part 1" and then it will be related to "How to: tutorial part 2".
My idea is that I can use a MySQL query to perform this, but looking around PHP may have some better functions. The table is 1300+ articles, so making this manually by selecting from a list what article it will be related is not a viable solution.
Cheers in advance.

Normalize your database, it will make this kind of stuff much easier. By this I mean make an own row where you write the id of the linked articles. Like:
article_group article_title friendly_url id
0 Article 1 article-1 013
0 Article 2 article-2 023
1 Example 1 example-1 034
1 Example 3 example-3 016
1 Example 2 example-2 015
Then you can have an own table for the name of each article group as well and other info on it. If the title is always the same, but it just have a number that differs them, then you can put that into the group and just keep the number in the above table, and is there any point to the friendly_url then? The latter questions are up to you though.

Related

How to update another row's column after update on a column

As the title says I want to automatically update another row's column after an update on a specific column.
I have this table
id username direct_referral indirect_referral total_referral referrer_id paid
---------------------------------------------------------------------------------
1 aj 100 56 156 1 1
2 john 100 40 140 1 1
3 michael 100 0 100 2 1
Now in this table refferer_id = id of referrer. For example aj has referred 'john' so he get 40% of the total_referral of john i.e.(140*40/100 = 56). john has referred michael so his indirect_referral will be 40% of michael i.e. (100*40/100 = 40).
Now I want to automatically increase indirect_referral of referrer by 40% of the total_referral of referral whenever a new user joins through his referral id and paid = 1.
Please tell me how can I do this process automatically and thanks in advance.
Your problem is tricky. But it might be solvable by a recursive SQL statement, also called CTE (common table expression).
There you have one initial query as an anchor member (the row you want to insert) and a recursive query that calls itself again and again.
Look at that link for an deeper explanation: https://www.mysqltutorial.org/mysql-recursive-cte/
Another way of solving your problem is to make a lot of SQL queries, that you manage by your PHP code.

SQL query to find all two word combinations

Using PHP and MySQL I need to find all "two word" combinations ordered by count from a table that has over a million rows.
The search needs to find how many instances of each two word combination, like "hardware store", "to the", "second chance", "for those", "senior citizens", etc.
Examples of text to search:
id | content
1 | The senior citizens went to the hardware store from the community center.
2 | The hardware store is offering a second chance drawing for those senior citizens who did not win.
3 | Many senior citizens go to the town's community center to play bingo.
Sample results:
senior citizens - 3
to the - 2
hardware store - 2
community center - 2
second chance - 1
The senior - 1
center to - 1
the town's - 1
etc ...and so on.
Results need to include all "two word" combinations. "The senior", went to", "the hardware", citizens went", etc., and the count of how many times found.
I'm guessing this might be a multiple query solution with sub queries, but my query building expertise is minimal. I have tried some basic queries, but am thinking the solution is going to be a bit more complex than my skill set.
Similar question with different data source.
Try a Union All join:
SELECT count(*) FROM your_table WHERE content LIKE '%senior citizens%'
UNION ALL
SELECT count(*) FROM your_table WHERE content LIKE '%to the%'
UNION ALL
SELECT count(*) FROM your_table WHERE content LIKE '%hardware store%'

category and subcategroy in same table

I have to create a table for category and subcategory in the same table.My table format will be as following:
Id Title Parent id
1. Spain None
2. Barcelona 1
3. India None
4. Delhi 3
Please give me mysql query for this.
Create a normal table in which the value of parent ID would be zero (0) for top level entities.
You may also keep the column nullable by having NULL for top level entities.
I would recommend to use zero for type safety.
Please use this as a starting point and not as a copy paste solution.
i think question already asked on stack overflow please refer this link
stackoverflow
stackoverflow
The first thing that you should do is read this article at mysql.com.
Managing Hierarchical Data in MySQL
or here
i hope this reference give you your solution.

Tracking a conversation in PHP MYSQL

Background: I have been working with twitter API. Have it setup so that when someone mentions/messages me, it gets sent to a chatbot. Bot make reply, I tweet it out using API.
Each tweet has a unique status_id in addition to a field called rep_to_stat_id so a conversation looks like this:
person 'hi how are you?' stat_id = 1 rep_to_stat_id = blank
me 'ok, and you?' stat_id = 2 rep_to_stat_id = 1
person 'are you stalking me' stat_id = 3 rep_to_stat_id = 2
me 'no you are stalking me' stat_id = 4 rep_to_stat_id = 3
etc.
When I send to bot first time, make a random converstation ID (conv_id) so the bot can track the conversation.
Problem is tracking this, it's like a ladder, leading up to the original stat_id with no rep_to_stat_id, and has to include the common conv_id.
Up until now I have always had tables with a one to many type setup, I cannot get my head around what type of structure this is.
Is there an option in mysql to add to query to follow a ladder of each conversation to its first/last point?
There are many ways to store hierarchical data in relational databases.
One of the most common ways is to simply have a table with all the columns you mentioned (stat_id, rep_to_stat_id [nullable], and conv_id). The problem is, to retrieve the full tree up to the root node, you'd need a recursive function (in MySQL or your scripting language of choice), making several queries.
This Stack Overflow question should give you a nice overview of all the available options for modelling your tree structure.
Have one column tag the conversation_id, and another that tracks the iteration within that conversation.
person text conversation_id reply_num
------------------------------------------------------------------
0 "Hi" 0 0
1 "Hello, who are you" 0 1
0 "I am a bot" 0 2
1 "Goodbye" 0 3
0 "Hi" 1 0 //new conversation thread
1 "who is this?" 1 1
0 "This is a bot" 1 2
1 "leave me alone" 1 3

MySQL/PHP Relating Database Content by Keywords

I read here a lot but this is the first I've asked a question.
I'm developing a website, with LAMP, that is essentially all about user submitted articles. I would like to relate some articles together by tags (or keywords) so that I may show a viewer some content that is related.
I had an idea of creating a field within the MySQL database table, where the articles resides, called "tags" that consists of a comma delimited list of keywords. They would relate to the article and describe it's content in some fashion. Yet I discovered this wasn't a bright idea as I wouldn't be able to index this very well.
So, how would I go about this, any ideas?
ps. Just seen the little box to the right on this site called, "Similar Questions" what I'm trying to achieve is a lot like that...
It's a many-to-many relationship, and can be modelled using a separate table with a foreign key to the article ID, and a column for a tag. Multiple tags are added to an article by adding multiple rows to the table.
For example, if you have two articles where:
Article 1 has tags "foo" and "bar" and
Article 2 has tags "bar" and "baz"
then the table might look like this:
article_tags
article_id tag
1 foo
1 bar
2 bar
2 baz
You can even store the tag names in a separate table:
tags
id name
1 foo
2 bar
3 baz
article_tags
article_id tag_id
1 1
1 2
2 2
2 3

Categories