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 8370569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:56:38+00:00 2026-06-09T13:56:38+00:00

This is my first ever question on SO. So I have a MYSQL table

  • 0

This is my first ever question on SO. So I have a MYSQL table named posts with the following columns:

post, tag1, tag2.

Let’s say that there are 10 total rows in the table. 9 of the posts have tags in tag1 only, and the tenth has a tag in tag1 and in tag2 (the tag in tag2 is duplicated in tag1 for other posts.) Heres what the table would look like:

  Tag1                Tag2
computer               -
tv                     -
computer               -
tv                     -
laptop                 -
bicycle                -
stereo                 -
computer             laptop
tv                     -
laptop                 -

The functioning that I want is to query the table to obtain the tags that appear with the highest frequency, group them together, and then order them in order of highest frequency to lowest. Here is my mysql query (I am using PDO):

SELECT count, tag 
FROM (SELECT count(tag1) as count, tag1 as tag FROM posts) as a 
UNION ALL 
SELECT count(tag2) as count, tag2 as tag 
FROM posts

This query does not work because it does not show me each of the tags that exists, and it also does not seem to be adding the one tag in column tag2 to the results from tag1, instead there is a duplication. So instead of seeing that laptop appears 3 times, I am seeing that laptop appears two times and then one time.

I figured this would be resolved by GROUP BY but when I attempt to add GROUP BY mysql throws a generic syntax error saying to look at the documentation for my database server. So I suppose I am not sure where to put the GROUP BY, and I am possibly thinking that my query is just not correct at all. Any help available please….ive looked at many other questions on using group by with union and just have not found the answer or am not understanding this enough to know when I see the answer.

  • 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-09T13:56:39+00:00Added an answer on June 9, 2026 at 1:56 pm

    I think you mean something like this (sqlfiddle):

    SELECT COUNT(*), tag
    FROM
    (
        SELECT Tag1 AS tag
        FROM posts
        UNION ALL
        SELECT Tag2 AS tag
        FROM posts
    ) AS alltags
    GROUP BY tag
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first post ever :). I have quite a bit of experience
Here's my HTML. Apologies for formatting - this is my first-ever question! <div id=Template>
this is my first ever question. I am developing a iPad app that allows
This is probably the worst question I have ever posted on Stack Overflow but
this is my first time ever posting a question and I am kind of
this is my first ever question on this website but it has helped me
This is my first ever post on any forum, so excuse my etiquette. Im
My first ever question here. Please excuse me, I have just entered into C++
To preface this question, this is my first app and I have a very
my question seems simple and stupid but first read this, suppose you have a

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.