mysql - SQL need help on a request -


i need on sql request. here tables:

table events:

id    |    idtype    |    used_by 1            1              marc 2            1                 3            1              henry 

table types:

id    |    name    |    activate 1         type 1           0 2         type 2           1 

as can see, have type 1 inactivate, 2 persons using it. display records events have type activated , display records used if type inactivated.

the sql below displays events have type activated:

create table events (id int(11), idtype int(11), used_by varchar(50)); insert events values (1, 1, ''), (2, 1, 'marc'), (3, 1, 'henry'), (4, 2, ''), (5, 2, 'sandy');  create table types (id int(11), name varchar(50), activate int(1)); insert types values (1, 'type1' , 0), (2, 'type 2', 1);  select events.id, types.name, events.used_by  events  inner join types  on events.idtype = types.id  types.activate = 1 

here sqlfiddle: sqlfiddle.com/#!9/7a36f/1 , result events ids: 2, 3 , 4 ,5

just add or condition used_by field:

select events.id, types.name, events.used_by events      inner join types      on events.idtype = types.id  types.activate = 1     or events.used_by != '' 

edited replace events.used_by check non-blank rather not null based on updated information.


Comments