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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:19:02+00:00 2026-06-13T11:19:02+00:00

I have one complicated question. I’ll try to explain it with example: have one

  • 0

I have one complicated question. I’ll try to explain it with example:

have one table that have primary key, and I want to join other table there the first’s table primary key is foreign key, and I want If in the second table there is duplicate foreign key to select the number of repeatability. For example:

1-st table:

  id    name 
  ---  -----
  1     Greg
  2     Alan 
  3    George 
  4     John 
  5     Peter   

2-nd table

 id       aid        data   
 ---     -----      -------
 1        2          CCCV   
 2        2          VVVV 
 3        3          DDDDD 
 4        3          SSSS 
 5        4          PPPPP 

I want the result of the join to be:

 id(1st table)  aid   name    Data   Number
 -----------    ----  -----   -----  -----
 1               null  Greg    null   1
 2                1    Alan    CCCV   1
 2                2    Alan    VVVV   2
 3                3    George  DDDDD  1
 3                4    George  SSSS   2
 4                5    John    PPPPP  1
 5               null  Peter   null   1

I searched a lot, I couldn’t find anything. Maybe I do not know how search, or there is no such thing as what I want to do.

  • 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-13T11:19:03+00:00Added an answer on June 13, 2026 at 11:19 am
    SELECT Table1.id, Table2.id as aid, Table1.name, Table2.data,
    GREATEST(1, (SELECT COUNT(*)
                 FROM Table2 t2
                 WHERE t2.aid = Table1.id
                 AND t2.id <= Table2.id))
    AS number
    FROM Table1
    LEFT JOIN Table2
    ON Table2.aid = Table1.id
    ORDER BY id, aid;
    

    works in both MySQL and PostgreSQL.

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

Sidebar

Related Questions

Hello I have a question that could seem complicated. But I will try to
I have a pair of static fields with a complicated one-time initialization. I want
This is a more general question: I have a quite complicated files table in
This question is complicated so examples would work best...I have the following table on
I have a simple question related to one-line programming. First an example: function test(a)
I have a follow up to complicated mysql question that I recently asked: Show
have one time consuming step that flattens a bunch of files. basically i'd like
I have one table which contains events and dates, and another which contains the
This is a pretty complicated question so be prepared! I want to generate some
this is almost right, it is not a very complicated question... I 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.