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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:09:16+00:00 2026-06-06T18:09:16+00:00

I have a table with 3 columns —QID—TEXT—CID— I would like to find 20

  • 0

I have a table with 3 columns

---QID---TEXT---CID---

I would like to find 20 rows(QID and TEXT) for each distinct CID. I have already prepared string $cid so that I can use WHERE IN statement.

SELECT * FROM questions q1
WHERE cid=(SELECT cid 
    FROM questions q2 
    WHERE q2.cid IN ($cids)
    GROUP BY q2.cid)
ORDER BY q1.qid LIMIT 20

Thank you!

  • 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-06T18:09:17+00:00Added an answer on June 6, 2026 at 6:09 pm

    Simple query:

    $query = 'SELECT QID, TEXT FROM yourDb.yourTable WHERE CID = '.$cid;
    

    or, if $cid is an array:

    $query = 'SELECT QID, TEXT FROM yourDb.yourTable WHERE CID IN('.implode(',',$cid).')';
    

    To get to the results:

    $pdo = new PDO('mysql:host=yourDBServer','login','password');
    if (!$stmt = $pdo->query($query))
    {
        die('query failed');
    }
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
    

    For more info on what you can do with the PDO object, refer to the manual

    A quick fix (but not a good one) might be:

    $q = 'SELECT QID, TEXT FROM yourDB.yourTB WHERE CID = '.$cid.' LIMIT 20';
    

    In the case of CID IN(1,2,3), I’m not sure if there’s a strait forward way of doing this. All I can think of is using unions. Mayby this page can help you out with that.

    A fugly fix might also be to ORDER BY CID ASC, and insted of using fetchAll(), do this:

    $query = 'SELECT CID,QID, TEXT FROM yourDb.yourTable WHERE CID IN('.implode(',',$cid).')';
    
    //execute query, same as above: $stmt holds results
    $results = array();
    while($row = $stmt->fetch(PDO::FETCH_ASSOC))
    {
        if (!is_array($results[$row['CID']))
        {
            $results[$row['CID']] = array();
        }
        if (count($results[$row['CID']]) < 20)
        {
            $results[$row['CID']][] = $row;
        }
    }
    

    This way, the $results array, will have a key for each CID that was found, and that key’s value will be an array of up to 20 records…

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

Sidebar

Related Questions

I have table with two columns: price and constant . I would like to
I have my table columns set like this: likes(id, like_message, timestamp) id is the
I have a table with about 35 million rows. each has about 35 integer
I have a table with columns like entityID, entityName, parentID How can I write
I Have Table with multiple columns and rows, Example, <table> <thead> <tr> <th>Head 1</th>
I have a table with columns: (project_id, name) It's a list of people, each
I have a table whose columns indicate a property of data items. Each item
If I have a table with columns like ID1 ID2 VAL1 VAL2 Where ID1,ID2
I have table which contains columns like SiteID (identity_Col) SiteName POrderID Location Address Cluster
I have a table with columns Name, EmailId, Process, Status like below. **Input** Name

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.