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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:44:37+00:00 2026-06-15T03:44:37+00:00

I have 2 tables tbltraining trId(PK) trname ——- —— 1 training1 2 training2 3

  • 0

I have 2 tables

tbltraining 

trId(PK)   trname 
-------    ------
1         training1 
2         training2
3         training3
4         training4
5         training5
6         training6


tbltrainAssign 

    trassigno    trId(FK)      date1           date2
    ---------    ------     -----------     ----------
    1            1          12/12/2012      12/12/2013
    1            1          12/12/2012      12/12/2013
    2            3          01/12/2012      08/12/2013
    2            3          01/12/2012      08/12/2013
    2            3          01/12/2012      08/12/2013
    3            1          02/12/2012      12/12/2013
    3            1          12/12/2012      12/12/2013

what i want is the data from tbltraining with the count of assignments. trId =1 is assigned 2 times and trId is assigned 1tim in tbltrainAssign . so the result should look like

trId        trName       count
1          training1     2
2          training2     0
3          training3     1
4          training4     0
5          training5     0
6          training6     0
  • 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-15T03:44:39+00:00Added an answer on June 15, 2026 at 3:44 am

    What you need is a simple, straightforward, JOIN especially LEFT JOIN to get unmatched trNames that had no assignment in the second table, with COUNT and a GROUP BY like so:

    SELECT t.trId, t.trname, COUNT(s.trId)
    FROM tbltraining t
    LEFT JOIN tbltrainAssign s ON t.trId = s.trId
    GROUP BY t.trId, t.trname;
    

    SQL Fiddle Demo

    Side not: Please avoid descriptive prefixes that you are using in the tables’ names and the columns’ names the tbl and the tr.

    Update: You have to select the distinct dates from the assignment table in order to count the distinct assignments for each training like so:

    SELECT t.trId, t.trname, COUNT(s.trId)
    FROM tbltraining t
    LEFT JOIN
    (
      SELECT DISTINCT trId, DATE(date1), date(date2)
      FROM tbltrainAssign
    ) s ON t.trId = s.trId
    GROUP BY t.trId, t.trname;
    

    Updated SQL Fiddle Demo

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

Sidebar

Related Questions

I have tables A and B with identity PKs ida and idb : ex
I have tables like these two test_table date student test 2012-05-31 Alice Math 2012-05-31
I have tables like this: tblUsers int UserID string UserName tblUsersInRoles int UserID int
I have tables linked by FK, I query on the first table using entity
I have tables named news and tags_news. Given a news id I have to
I have tables Product ( ID, CategoryID, ... ) Category ( ID, Name, ..
In the database we have tables called Sites, Organisations, Person, Department have the same
I used to have tables before to display the content and people here advised
hi i have tables like this: Persons: person_id, name then i have many langauge
Our client's request is to have tables in PDF with rounded corner. I only

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.