Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is a common theme in the comments here and that is <a href="http://en.wikipedia.org/wiki/Database_normalization" rel="nofollow">normalization</a>. As a rule it is A Bad Thing to represent multiple values as a comma-separated list in a single column.</p> <p>Here is a working example of an alternate DB design that should get you going in the right direction:</p> <pre><code>create table staff ( id int unsigned not null primary key auto_increment, staffName varchar(250), unique key `staffUIdx1` (staffName) ) ENGINE=InnoDB; create table business ( id int unsigned not null primary key auto_increment, businessName varchar(250), unique key `staffUIdx1` (businessName) ) ENGINE=InnoDB; CREATE TABLE staffBusiness ( id int unsigned not null primary key auto_increment, staffId int unsigned not null, businessId int unsigned not null, unique key `staffBusinessUIdx1` (staffId,businessId), CONSTRAINT `fk_staffBusiness_staff1` FOREIGN KEY (`staffId`) REFERENCES `staff` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT `fk_staffBusiness_business1` FOREIGN KEY (`businessId`) REFERENCES `business` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB; insert into staff (staffName) values ('Bob'); insert into staff (staffName) values ('Bill'); insert into staff (staffName) values ('Paul'); insert into staff (staffName) values ('George'); insert into business (businessName) values ('Business 1'); insert into business (businessName) values ('Business 2'); insert into business (businessName) values ('Business 3'); insert into business (businessName) values ('Business 4'); insert into staffBusiness (staffId,businessId) select s.id,b.id from staff s join business b where s.staffName = 'Bob' and b.businessName in ('Business 1','Business 4'); insert into staffBusiness (staffId,businessId) select s.id,b.id from staff s join business b where s.staffName = 'Bill' and b.businessName in ('Business 2','Business 3'); insert into staffBusiness (staffId,businessId) select s.id,b.id from staff s join business b where s.staffName = 'Paul' and b.businessName in ('Business 3'); </code></pre> <p>...and then the query would look like this:</p> <pre><code>select staffName as Staff,count(sb.id) as Business from staff s left outer join staffBusiness sb on s.id = sb.staffId group by staffName; </code></pre> <p>I've included a 4th member of staff called 'George' to show that a normalized approach allows you to have members of staff with no business too.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload