group per SQL query

Asked

Viewed 119 times

0

I have a table post with a field category, each post that is inserted may receive more than one category:

post1 categoria1, categoria2
post2 categoria1, categoria3

how can I perform an sql query to join the duplicate categories and have a result like this:
categoria1, categoria2, categoria 3
instead of
categoria1, categoria2, categoria1, categoria 3

SELECT post_category FROMpostsgroup by post_category

outworking: Empresas, Ecônomia, ESPN, Ecônomia
I have only two posts each with 2 categories

  • Could you post an example of SQL that you are using so that other users can use it as a basis to formulate a good answer? I already say that the first thing I thought of as a solution is to add a DISTINCT in his SELECT.

  • I think that DISTINCT won’t work because the categories are within the same field, don’t know much about mysql, just simple searches

  • Is this column multivariate and in the first normal form? If it is, then this is going to be complicated. If it is not, then explain it better as it is. See more details at the beginning of this answer: http://answall.com/a/151394/132

  • yes, it’s multivariate, in which case I’ll create another table

1 answer

-2

The correct thing would be for you to have the following structure.. post category post1 categoria1 post1 categoria2 post2 categoria1 post2 categoria3 then you could easily list the distinct categories of each post.. But in this case, as I understand it, you’re entering more than one category into the same field and all concatenated. But as the structure is already assembled you can use the splifn function of this link here and use the function to generate a table and bring in the distinct ones of each post.

  • So Robson actually already solved the problem, as @Victor Stafusa said, what I thought was correct was to normalize the database and turn this field "Categories" in another table so I facilitate queries in general!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.