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

  • Home
  • SEARCH
  • 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 7413889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:58:20+00:00 2026-05-29T06:58:20+00:00

I have two tables. Table Emp id name 1 Ajay 2 Amol 3 Sanjay

  • 0

I have two tables.

Table Emp

    id   name
    1    Ajay
    2    Amol 
    3    Sanjay 
    4    Vijay

Table Sports

Sport_name     Played by
Cricket        ^2^,^3^,^4^
Football       ^1^,^3^ 
Vollyball      ^4^,^1^

Now I want to write a query which will give me output like

name   No_of_sports_played 
Ajay          2
Amol          1
Sanjay        2 
Vijay         2

So what will be Mysql query for this?

  • 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-29T06:58:21+00:00Added an answer on May 29, 2026 at 6:58 am

    I agree with the above answers/comments that you are not using a database for what a database is for, but here is how you could calculate your table from your current structure in case you have no control over that:

    SELECT Emp.name, IF(Played_by IS NULL,0,COUNT(*)) as Num_Sports
    FROM Emp
    LEFT JOIN Sports
    ON Sports.Played_by RLIKE CONCAT('[[:<:]]',Emp.id,'[[:>:]]')
    GROUP BY Emp.name;
    

    See it in action here.
    UPDATE: added the IF(Played_by IS NULL,0,COUNT(*)) instead of COUNT(*). This means that if an employee doesn’t play anything they’ll have a 0 as their Num_Sports. See it here (I also added in those ^ characters and it still works.

    What it does is joins the Emp table to the Sports table if it can find the Emp.id in the corresponding Played_by column.

    For example, if we wanted to see what sports Ajay played (id=1), we could do:

    SELECT *
    FROM Emp, Sports
    WHERE Sports.Played_by LIKE '%1%'
      AND Emp.id=1;
    

    The query I gave as my solution is basically the query above, with a GROUP BY Emp.name to perform it for each employee.

    The one modification is the use of RLIKE instead of LIKE.

    I use RLIKE '[[:<:]]employeeid[[:>:]]' instead of LIKE '%employeeid%. The [[:<:]] symbols just mean “make sure the employeeid you match is a whole word”.

    This prevents (e.g.) Emp.id 1 matching the 1 in the Played_by of 3,4,11,2.

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

Sidebar

Related Questions

I have two tables: Table 1: ID, PersonCode, Name, Table 2: ID, Table1ID, Location,
I have two tables: Table items: | id | name | description | |
I have two tables: EMP emp_id | Name | Surname | 1 | Bob
Suppose I have two tables 1st table emp EmpID | EmpName|xyz...coloums 1. | Hrishi
I have two tables: Table alpha: ID | Name --------- 1 | A 2
Suppose i have two tables declare @emp table ( EmpID int, EmpName varchar(10) )
I have two tables Table : 1, Name : NdcAwp, Columns-Ndc, AwpUnitCost Table :
I have two tables Table Visitor ID Name CityName 1 Jon NY 1 Jon
Suppose I have a dataset with those two immortal tables: Employee & Order Emp
I have two tables Table public.tags_to_entities Column | Type | Modifiers --------+---------+----------- tid |

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.