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:

  1. more complex needed,
  2. eventually inefficient data grows in size,
  3. open inconsistency because can't define referential constraints properly,
  4. 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

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -