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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:16:19+00:00 2026-05-24T07:16:19+00:00

I have table prod with structure Id, name, keyword, yearborn. It is necessary to

  • 0

I have table prod with structure
Id, name, keyword, yearborn.

It is necessary to bring the number of keywords grouped encountered in the field yearborn , for example

SELECT yearborn, COUNT(Id) cnt1 FROM prod WHERE keyword='admiral'
GROUP BY yearborn

and

SELECT yearborn, COUNT(Id) cnt2 FROM prod WHERE keyword='captain' GROUP BY yearborn

How do I combine these two queries to get the output result table with the structure

yearborn, cnt1, cnt2?

I have tried UNION

SELECT yearborn, COUNT(Id) cnt1 FROM prod WHERE keyword='admiral' GROUP BY yearborn
UNION
SELECT yearborn, COUNT(Id) cnt2 FROM prod WHERE keyword='captain' GROUP BY yearborn

But result is:
yearborn, cnt1

Tell me please how to get out of this situation.

  • 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-05-24T07:16:20+00:00Added an answer on May 24, 2026 at 7:16 am
      SELECT yearborn, 
             SUM(IF(keyword = 'admiral', 1, 0)) cnt1, 
             SUM(IF(keyword = 'captain', 1, 0)) cnt2 
        FROM prod 
       WHERE keyword = 'admiral'
          OR keyword = 'captain'
    GROUP BY yearborn
    

    The result will be something like this (of course this is fictitious data):

    +----------+------+------+
    | yearborn | cnt1 | cnt2 |
    +----------+------+------+
    |     2004 | 1233 |    0 |
    |     1999 |    0 |   15 |
    +----------+------+------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table in data base name train delay, with columns train number(int), DelayTime(int),
i have a table of the following structure id name field1 field2 field3 1
I have table with 50 entries (users with such details like Name Surname Location
i have table structure with 3 colums (column1, column2, column3) and i want to
I have there mysql table: **product (id,name)** 1 Samsung 2 Toshiba 3 Sony **attribute
I have a products sales table that looks like this: saleDate prod qty 10/22/09
I have a table structure Table1 ID Hours Qty ProductID 1 2 1 100
I have a table name Product and another table name category. Product table has
I have a table tbl which looks like this: prod | cust | qty
I have a table like this: Product sl_no Wt_Kg Prod-001 1 3.000 Prod-002 2

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.