• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

MS SQL Query

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to invoke rows from a table in such a way that ,
suppose employeeAccess is my table i want to fetch all the records from employeeAccess table which
satisfy the following criteria as

if accessid = 1 then accessval = 'all'
<b>And</b>
if accessid = 2 then accessval = 'read'

I don't want to make the condition either or.

Suppose my table has some values like -

Empidaccessidaccessval
11all
12write
13read
14all

21all
22read
23write
24read

31read
32read
33write
34all

In the above case i only want to fetch the empid 2 . Neither 1 nor 3, as only
empid = 2 satisfy both condition that is for
empid = 2 when accessid = 1, accessval is 'all'
and when accessid = 2, accessval is 'read'
empid = 1 record is not expected, as in this case
when accessid = 1, accessval is 'all'
but when accessid = 2, accessval is something other than 'read'
empid = 3 record is also not expected, as in this case
when accessid = 2, accessval is 'read'
but when accessid = 1, accessval is something other than 'all'

Please, help me outof this.
Note: I can't change the table format.
 
author
Posts: 4229
33
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could probably solve this using an inner join of the table on itself with the empid field as the join condition such as:

Keep in mind there might be a shortcut I'm missing (its monday morning) and if the employee table is very big, joining on itself might not lead to the best performance.
 
Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic