Having the following tables
CREATE TABLE contacts (
Id INT NOT NULL AUTO_INCREMENT,
Name varchar(255) NOT NULL,
PRIMARY KEY(Id)
);
CREATE TABLE contacts_cstm (
Id INT NOT NULL AUTO_INCREMENT,
Id_c INT NOT NULL,
Department_c varchar(255) NOT NULL,
PRIMARY KEY(Id)
);
And having contacts_cstm
data like this
Id Id_c Department_c
1 1 ^pro^
2 2 ^pro^,^pro^
3 3 ^pro^,^temp^,^pro^
I want to count all the data in the contacts_cstm table. where the value of the department_c
field includes ^pro^, or where there is more than one incidence of ^pro^ like the second row, but it should not participate in the account if it includes other items different from ^prop^
The expected output is 2, for registers that include ^pro^
and ^pro^,^pro^
I have created this sqlfiddle
Thanks in advance
2
Answers
using replace to identify exclusions
https://dbfiddle.uk/5NVZIHaL
Your structure does not make any sense. Storing a comma separated list of departments in
Department_c
is obviously a bad idea (see Is storing a delimited list in a database column really that bad?) and requires string manipulation and a full table scan to get the answers you want. To make this even worse, you have stored this serialised relationship in a separate table with a meaningless surrogate PK, when you already haveId_c
(presumably FK tocontacts.Id
?).It is not totally clear from your question what you are trying to do here, but it seems likely that this data would be better represented by
contacts (Id, Name)
,departments (Id, Name)
andcontact_department (contact_id, department_id)
. This can be queried much more efficiently as it can effectively use indices, instead of requiring full table scans.That said, with your current structure, you can improve on P.Salmon’s answer slightly by removing the need for the index scan for the outer
count(*)
:This assumes that you would not have a row in
contacts_cstm
wheredepartment_c
is an empty string. If the empty string is a possible scenario, you can add a criterion to exclude the empty strings:Here’s a db<>fiddle.