ruby - To select a column from a table using values from another table -
i have tables columns below:
employee:
id number(10), name varchar2(10)
department:
id number(10), name varchar2(10), emp_ids varchar2(10)
the values present in tables respectively are:
employee:
1,abc,111 2,def,222 3,xyz,333
department:
111,development,'1,2' 222,testing,'2,3'
my problem need select employee names using emp_ids
column department table.
example:
select names employee id in (select emp_ids department name = 'development');
since emp_ids
varchar2 datatype unable execute above command.
note: using activerecord base connection
connect oracle db ruby, including ruby in tags too.
your data model not designed. could build query work be:
- more complex needed,
- eventually inefficient data grows in size,
- open inconsistency because can't define referential constraints properly,
- most vendor specific.
i suggest use reliable data model. have n-n relationship, need 3 tables:
employee (id number(10) primary key, name varchar2(10)) department (id number(10) primary key,name varchar2(10)) employee_department ( emp_id number(10) references employee (id), dep_id number(10) references department(id), constraint pk_emp_dept primary key (emp_id, dep_id) )
then query work flawlessly, efficiently , won't lose sleep @ night regarding data inconsistency.
select names employee id in (select ed.emp_id department d join employee_department ed on d.id = ed.dep_id d.name = 'development');
as database developer 2 main goals are:
- to make sure data saved database. nothing else more important data. if can't trust data, database? eliminating inconsistencies top priority.
- to make bug-free application. if model appropriate, queries simpler, make less bugs, applications faster , in general more reliable.
Comments
Post a Comment