in reply to Help with MySQL SELECT into multidimensional array

Any reason you couldn't do it in one query with outer joins?
  • Comment on Re: Help with MySQL SELECT into multidimensional array

Replies are listed 'Best First'.
Re^2: Help with MySQL SELECT into multidimensional array
by btongeorge (Initiate) on Dec 02, 2011 at 17:53 UTC

    It might be possible, and I did start down that road, but the query got pretty scary. I think I'm more comfortable with multiple queries, but maybe I'm wrong. Here are all the queries, the first generates the client list, the rest populate the integer fields:

    SELECT c2.id, c2.name as 'client' FROM client c2 WHERE level = 50 and status = 1; SELECT c2.id, c2.name as 'client', count(*) as 'ext' FROM client c1, client c2 WHERE c1.level=100 AND c1.status=1 AND ext.type = "phone" AND c1.parent_client_id = c2.id GROUP BY c1.parent_client_id ORDER BY c2.name; SELECT c2,id, c2.name as 'client', count(*) as 'vm' FROM extension ext, client c1, client c2, extension_prefs exp WHERE ext.client_id = c1.id AND c1.level=100 AND exp.param="voicemail" AND exp.value=1 AND c1.parent_client_id = c2.id AND exp.extension_id = ext.id GROUP BY c2.name; SELECT c2.id, c2.name as 'client', count(*) as 'ivr' FROM extension ext, client c1, client c2 WHERE ext.client_id = c1.id AND ext.type = 'ivr' AND c1.status = 1 AND c1.parent_client_id = c2.id GROUP BY c2.name; SELECT c2.id, c2.name as 'client', count(*) as 'queues' FROM extension ext, client c1, client c2 WHERE ext.client_id = c1.id AND ext.type = 'queue' AND c1.status = 1 AND c1.parent_client_id = c2.id GROUP BY c2.name; SELECT c2.id, c2.name as 'client', sum(if(value<11,1,0)) AS "conf10" FROM extension ext, client c1, client c2, extension_prefs exp WHERE ext.client_id = c1.id AND exp.param="conf_size" AND ext.type="conference" AND c1.parent_client_id = c2.id AND exp.extension_id = ext.id GROUP BY c2.name; SELECT c2.id, c2.name as 'client', sum(if(value<20 AND value>10,1,0)) AS "conf20" FROM extension ext, client c1, client c2, extension_prefs exp WHERE ext.client_id = c1.id AND exp.param="conf_size" AND ext.type="conference" AND c1.parent_client_id = c2.id AND exp.extension_id = ext.id GROUP BY c2.name; SELECT c2.id, c2.name as 'client', sum(if(value>20,1,0)) AS "conf30" FROM extension ext, client c1, client c2, extension_prefs exp WHERE ext.client_id = c1.id AND exp.param="conf_size" AND ext.type="conference" AND c1.parent_client_id = c2.id AND exp.extension_id = ext.id GROUP BY c2.name;
      I would consider making views out of the other queries (then the main query and the joins are simple), but I would also probably change the queries. The usual thing to do is to include everything but the aggregate in the GROUP BY clause, but I'm not sure what MySQL does when you don't do that and I don't know, e.g., what the keys of the tables are or if you have duplicate client name's with multiple client id's and how you want to handle that, etc.

        Very interested in your comments re. changing the queries. Haven't ever used views in MySQL so will look into that. There are no duplicate clients in the table and there won't be, the client ID is the primary key.

        How do you suggest that I modify the queries? Not sure I fully understand the implications of your comments.