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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:26:49+00:00 2026-06-05T08:26:49+00:00

Okay, I have a single table like this: id query results ip ——————————— 1

  • 0

Okay, I have a single table like this:

id  query  results  ip
---------------------------------
1   milk   17       10.10.10.1
2   milk   17       10.10.10.1
3   milk   17       10.10.10.2
4   bread  8        10.10.10.2
5   bread  8        10.10.10.2
6   cheese 12       10.10.10.1

I have this query right now:

SELECT COUNT(DISTINCT(id)) as cnt, query, results, ip
FROM table
GROUP BY query
ORDER BY results ASC
LIMIT 20

This query returns these results:

count  query  results
---------------------
2      bread  8
1      cheese 12
3      milk   17

I need to expand on that query (or redo it completely) to return something like this, preferably with the IP’s in a nested array as part of the query’s array:

query  count  results  ip
--------------------------------
bread  2      8        10.10.10.2 (2)
cheese 1      12       10.10.10.1 (1)
milk   3      17       10.10.10.1 (2)
                       10.10.10.2 (1)

I’ll be outputting this with php, attaching the IP list with that count to the query count display (click to show/hide a modal with the info populated).

Any direction or help to get the data I need out would be wonderful!

  • 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-05T08:26:53+00:00Added an answer on June 5, 2026 at 8:26 am

    The answer to this (despite numerous claims to the contrary) is “Yes you can, with a derived table”:

    SELECT
      SUM(cnt) as cnt,
      query, results,
      GROUP_CONCAT(ip SEPARATOR '\n') AS ip
    FROM (
      SELECT
        query, results,
        COUNT(DISTINCT(id)) as cnt,
        CONCAT(ip, '(', COUNT(ip), ')') AS ip
      FROM test
      GROUP BY query, ip
    ) t
    GROUP BY query
    ORDER BY results ASC
    LIMIT 20
    

    Sample…

    The one thing you cannot do is have PHP understand that the value of ip is an array with no other interference. However, you could do this slightly nasty thing to dynamically construct the data into JSON, and then it can simply be passed to json_decode() in PHP to easily convert it to a PHP vector type.

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

Sidebar

Related Questions

Okay right now I have too include the code below on every single page
Okay i've seen this done somewhere before where you have a function that takes
Okay, so I have this page, which has one Ajax:AsyncFileUploader also some other ajax
Okay trying to construct a single query to save myself a whole bunch of
Okay we have a single - sign - on and the user will likely
Okay, so I need to have the output look like a diamond of asterisks,
I have a query that I am currently getting the ORA-01427: single-row subquery returns
Hello there okay so this is the deal i have a folder where my
Okay, I have a multi-dimensional array which is statically-allocated. I'd very much like to
Okay, so this is a bit confusing (well to me). I have a string

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.