All pastes #471401 Raw Edit

liquidengineer

public sql v1 · immutable
·published 2007-05-04 21:32 UTC
rendered paste body
I have two tables as defined below.CREATE TABLE `userlevels` (  `level` tinyint(1) unsigned NOT NULL,  `levelname` varchar(32) NOT NULL,  PRIMARY KEY  (`level`)) ENGINE=InnoDB DEFAULT CHARSET=utf8CREATE TABLE `users` (  `username` varchar(32) NOT NULL,  `password` varchar(32) NOT NULL,  `userid` varchar(32) NOT NULL,  `userlevel` tinyint(1) unsigned NOT NULL default '1',  `email` varchar(50) NOT NULL,  `timestamp` int(11) unsigned NOT NULL,  PRIMARY KEY  (`username`),  KEY `userlevel` (`userlevel`),  CONSTRAINT `users_ibfk_1` FOREIGN KEY (`userlevel`) REFERENCES `userlevels` (`level`) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE=InnoDB DEFAULT CHARSET=utf8I would like to create a table that, for each row in userlevels adds a column with a count of how many times that level appears in users.  Where a level does not appear in users, a zero should be inserted into the column.