I have a MySQL database-table with the following colums
ID
status (can contain values 0, 1, 2)
timepstamp
text
note
owner
I’d like to obtain the following information about the entries of aspecific owner from the table:
number of entries
number of entries where status=0
number of entries where status=1
number of entries where status=2
number of entries where LENGTH(note)>0
minimum timestamp
maximum timestamp
I used to read the complete datasets and then evaluate them with PHP using
SELECT status, timestamp, LENGTH(note)>0 WHERE owner="name";
I have the problem that some users have so many entries, that that I frequently get an out of memory error if I read the data to PHP. I thought that letting MySQL evaluating the data should be more performat. I could not manage to write a query that could fulfill this task.
SELECT
MIN(timestamp) AS mintime,
MAX(timestamp) AS maxtime,
COUNT(*) AS number,
...
WHERE owner="name"
Is it somehow possible to obtain the result in one go? For example with a nested WHERE or IFwithin a COUNT?
COUNT(WHERE status=0) AS inactive
COUNT(IF(status=1)) AS active
...
How would you solve the problem?
Give this a try –