If new options are likely to appear and you don't filter by the options, you may as well go with a EAV
structure (a record per option).
This way, you can add new options more easily (no change to metadata).
Assuming that the options values are either TRUE
or FALSE
(no NULL
possible), you should create records only for non-default option values (TRUE
in your case). An absence of the record would mean false.
To retrieve all options, you could use this:
SELECT *, GROUP_CONCAT(CONCAT(o.id, ': ', ov.user IS NULL), ', ' ORDER BY o.id)
FROM users u
CROSS JOIN
options o
LEFT JOIN
option_value ov
ON (ov.user, ov.option) = (u.id, o.id)
GROUP BY
u.id
, which would give you dynamic output:
user_id options
1 1: 0, 2: 1, 3: 0
condition
column and put only these options_id which are true. – porton Jan 17 '13 at 10:21