How to find most duplicate value in a table? - mysql

The question basically means is how can we find duplicate values in a row?
Example: I am making a blog website and in that, I want to display trending tags...
For this I created 2 tables:
blog
tag
in the blog table, my blog record is there, and in the tag table there are two columns
id (PRIMARY)
name
blog_id
I want to display the top repeating names that come in the tag table.
So, can you please just tell me what is the solution about it?
Like here Java repeats most time in the table.
So, I just want the output to be like:
JAVA
Database
PHP

Related

How to update a MySQL table with information from another table without having repeats?

I am not very familiar with MySQL, so forgive me if this is a stupid question.
I have 2 tables: content and category.
content has a variety of columns with information, but I am interested in the category column.
category basically is just the category column from the first table.
Now, content can be anything, even repeats. I want category to be one of each and no repeats. So if something new is added to content then it adds it to category otherwise do nothing.
Right now content has many rows while category is empty.
What is the best way to go through all of content this one time and add one of each to category?
And if possible; update category as new entries are made to content (checking if it already exists and if not add)?
MySQL version: 5.7.28
phpMyAdmin version: 4.9.2
There are multiple ways, maybe using only sql, as stated here (https://www.w3schools.com/sql/sql_insert_into_select.asp):
INSERT INTO category (column1, column2, column3, ...)
SELECT content.category
FROM content
WHERE condition;
Due to the fact that we don't know the structure of the second column I don't know how to help you more than that.
Maybe you can also do something like :
SELECT category FROM content
map the data to fit into the new table e filter the duplicates e then :
INSERT INTO category VALUE(?,?,?,..)
Certain works are better if done in the backend instead of taking the logic into the db
The same thing for the update category as new entries are made to content:
insert data into category table after you do that into content table, and only if everything is ok put it into category table.
Don't use macro or any plugins, don't automate it in a place where you're not absolute sure you can check things if something happens

database model: group similar images together

I've created a db in mySQL that stores many things, but specially images. The table name for storing the images is image, like so:
image (image_id, title, caption, filename, published_date, ...)
It's been almost 2 years and i've uploaded almost 5000 images into the table.
Now, i want to add a new functionality. I want to group similar images so when im looking at an image, i can also have the option to look at images that are similar.
I'm not sure if i need a new table or should i use the same table or both. Any ideas/suggestions on how it should be?
You should create another table with that similiarities.
Why can't it be the same table ? One record in You table can have many similiar records (so it will be adding many columns to that table or for every similiarity there will be another row in YOur table. So the only logical option is to create another table.
IdFromMainTable | IdOfSimiliarRecord
Later on You can show that similiarites in a view easily joining that table by IdFromMainTable. Or both IdOfSimiliarRecord and IdFromMainTable. [depends if u want to add 2 records for similiar records or just one for similiar pair]
It sounds like what you want to do is create tags for the images. There are a number of ways to do that, but adding the tags to the same table would prevent a whole lot of joins from taking place and would likely be faster. You could just store the tags as JSON (if you're using MySQL =>7.5.8).

Different views for different users

Say I have a database with 3 tables: Stream, Tag and Post
Stream has one tag. Multiple streams can have the same tag.
Stream is owned by one user.
A tag has many posts.
Different tags can have the same post.
Each user that opens his stream gets it's posts (via the tag) and can delete (or hide) the posts he doesn't want to see (but only for him, not for others).
What would be the appropriate way to achieve this? Obviously I'll need some kind of soft-delete flag on posts. I currently have 2 ideas in mind:
Create another table that will act as the view of the stream with columns stream_id, post_id and is_deleted
Add deleted column to Stream that holds a JSON array of deleted posts' ids. The problem with this approach is querying based on is_deleted state.
Are there any better ways?
Note: Currently, I need Tag to stay in it's own table, rather than storing it as text in a column in Stream
You should have a new table with the columns post_id, is_deleted & user_id.
In this way you can manage it easily or else it will be hectic work as per your options are considered.
The table name will be like cross reference table ex: user_deleted_posts_xref

MySQL: - Bulk insert category to all posts in wordpress

I am searching for a MySQL query that via PHPmyAdmin inserts a category (Not modify existing) to all existing posts. Bulk edit in Wordpress dashboard doesn't work for me because of high server load, I have more than 2000 posts and I need to add an another category "Italy" to all of them and do not remove existing categories (italian cities). Can anyone help?
I've found some useful information for your problem:
wp_categories
If you have any categories in your WordPress installation, wp_categories is the table that keeps those records. Category names and descriptions are stored here, as well the ID of each category’s parent.
To work faster, WordPress often keeps aggregated values in the database, instead of recalculating them each time. For example, frequently requested counts of posts and links in each category are simply stored in the wp_categories table (WordPress uses the same set of categories for both links and posts). Every time you add post to a category, the post counter (column category_counter) increases. Every time you remove post from the category, the counter decreased. Same goes for links (column link_count). That’s why you see those extra columns in the table.
wp_post2cat
Linking posts to categories is done via wp_post2cat table. This is a standard approach for many-to-many relationships in relational databases. Thewp_post2cat table has only three fields: the unique record ID (automatically generated), the ID of the post, and the ID of the category to add the post to.
So if you could just give us 1 table row of the "wp_categories" we could easily create a query for bulk actions.
source: http://wpbits.wordpress.com/2007/08/08/a-look-inside-the-wordpress-database/
cheers.

mysql db structure for better performance

I'm not a professional, but I'm messing around with wordpress trying to customize it the way I want it to work...
I've a necessity to add additional data to single-posts, specifically a restaurant menu, each post will have unique menu displayed in sidebar.
Single post is a single row in wp_posts table.
I'm adding additional info using wp metabox functionality, which has its own table called wp_postmeta.
This table is different from wp_posts because it can contain many rows of meta information for a single post.
My questions:
Is this implementation slow?
Will it cause problems if db grows?
Why would wp guyz implement this in this way?
Would not it be wiser to have single meta column and multiple rows per post?
I see that you have "price_list_heading", "price_list_category","price_list_heading1", "price_list_category1","price_list_heading2", "price_list_category2","price_list_heading3" and "price_list_category3" I don't know why, but I think you shouldn't do that, you need to create a relational table with index (I don't know if Wordpress let you do that)
it'd will look like: this will depend of the cardinality of your APP
Let's say that 1 post meta can have multiple categories and multiple categories can be with one postmeta (cardinality N:M)
Now If one post can have multiple categories and one category can have only one post
Where wp_postmeta_copy1 is a FK from ws_postmeta table (id)
Please take a look to this documentation :
http://en.wikipedia.org/wiki/Entity%E2%80%93relationship_model
http://www.inf.unibz.it/~franconi/teaching/2000/ct481/er-modelling/
http://searchsqlserver.techtarget.com/definition/entity-relationship-model