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!
The answer to this (despite numerous claims to the contrary) is “Yes you can, with a derived table”:
Sample…
The one thing you cannot do is have PHP understand that the value of
ipis 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 tojson_decode()in PHP to easily convert it to a PHP vector type.