Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8840181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:24:25+00:00 2026-06-14T10:24:25+00:00

I am doing some data analysis in MYSQL and hoping that the following is

  • 0

I am doing some data analysis in MYSQL and hoping that the following is possible.

I have a table that documents user blog topics as follows. I have a total of 25 topics, and would like to abstract this enough that we can add topics later and not have to redo the query (if possible):

-----------------------
| user_id |  topic    |
-----------------------
|   01    |  art      | 
|   01    |  cooking  |
|   02    |  art      | 
|   03    |  outdoors |
|   03    |  art      |
|   03    |  cooking  |
-----------------------

In order to create a chord diagram (http://bl.ocks.org/4062006) from this data, I need to analyze the relationships between any two topics, either by grouping the rows by two topics, or by creating a matrix:

-----------------------------------------
| topic_combo            |  user_cnt    |
-----------------------------------------
|   art + cooking        |  2           | 
|   art + outdoors       |  1           |
|   cooking + outdoors   |  1           | 
-----------------------------------------

Or, even better…

---------------------------------------------
|          |  art  |  cooking  |  outdoors  |
---------------------------------------------
| art      |   3   |    2      |     1      |
| cooking  |   2   |    2      |     1      |
| outdoors |   1   |    1      |     1      |
---------------------------------------------

Are either of these possible in MYSQL? If so, what’s the easiest way to do them?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:24:26+00:00Added an answer on June 14, 2026 at 10:24 am

    Well, I do not know if this is the best way but at least it is one until you get a better answer. Use JOIN and GROUP BY as mentioned by Guvante.

    SELECT a.topic AS at, b.topic AS bt, COUNT(*) AS c FROM a JOIN a AS b ON a.user_id=b.user_id GROUP BY a.topic, b.topic;
    

    This will result in an output like this:

    at          bt          c
    -----------------------------
    art         art         3
    art         cooking     2
    art         outdoors    1
    cooking     art         2
    cooking     cooking     2
    cooking     outdoors    1
    outdoors    art         1
    outdoors    cooking     1
    outdoors    outdoors    1
    

    You can transform it into a matrix e.g. by PHP

    while($data = mysql_fetch_object($sql)) {
      $matrix[$data->at][$data->bt] = $data->c;
    }
    

    Edit: Removed duplication as mentioned in the comments after a hint of a friend.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web application that will be doing some processing with submitted data.
I have been doing some data analysis in R and I am trying to
I'm doing some analysis on user keystroke patterns for a biometrics-related prototype that I'm
I'm doing some analysis something like this: library(plyr) input.files <- c(file1.txt, file2.txt, file3.txt) input.data
I'm doing some writing of data to my documents directory and I was wondering
I've got some analysis code ( myprog ) that sucks in data using the
I'm doing some data analysis over the judge appointment system in France, and using
I am doing some testing where I have a lot of files that I
I have a process running every night and doing lot of data analysis for
So I am doing some data analysis in which I am required to extract

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.