EXISTS SQL QUERY [message #635025] |
Thu, 19 March 2015 05:05 |
|
palpali
Messages: 138 Registered: December 2014 Location: India
|
Senior Member |
|
|
Hallo
I would like to share my Problem and would like to have your Suggestion and help.
I have two tables and and data in it (which i have pasted here for your reference)
and i am trying to make a query, which gives me Output as:
when the logistic.status = I then it should Display all the Logis
when the logistic.status = A then it should Display all other Logis but not that which has the statuis is A.
my code:
create table Testbooks (
book_id number,
book_name varchar2(30),
preis number,
logis number
);
insert into Testbooks (book_id, book_name, preis, logis) values (45, 'Oracle Tools', 80, 2);
insert into Testbooks (book_id, book_name, preis, logis) values (50, 'Oracle for Beginners', 120, 5);
insert into Testbooks (book_id, book_name, preis, logis) values (60, 'Advanced SQL', 50, 2);
insert into Testbooks (book_id, book_name, preis, logis) values (55, 'Advancd Oracle', 150, 5);
insert into Testbooks (book_id, book_name, preis, logis) values (35, 'JAVA Beginners', 60, 2);
commit;
create table logistic (
logis number,
s_loc varchar2(20),
status varchar2(1)
);
insert into logistic (logis, s_loc, status) values (2, 'ABC', 'A'); -- A = Active
insert into logistic (logis, s_loc, status) values (1, 'MOON', 'I'); -- I = Inactive
commit;
and i have tried with this query but could not get the Output of my requirement :/
select Testbooks.logis from Testbooks where exists (select logis from logistic where status = 'I');
I thank you all in advance.
|
|
|
|
|
|
|
|
|
|
|
Re: sql [message #635045 is a reply to message #635043] |
Thu, 19 March 2015 07:35 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
If you mean to apply those rules while fetching rows from both the tables using SQL, then you would anyway get all the rows since there exists a row in logistic table with the status as 'I'. SO, you need to be clear that how you want to apply this condition? From where are you expecting the value to be applied in the filter predicate? Are these values to be passed via application? Or, you just want to query the tables to get desired rows? IT is not at all clear what you want.
|
|
|
|
Re: sql [message #635048 is a reply to message #635046] |
Thu, 19 March 2015 08:04 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
palpali wrote on Thu, 19 March 2015 18:09
yes i want to query the tables to fetch the rows as per condition,
Lalit Kumar B wrote on Thu, 19 March 2015 18:05you would anyway get all the rows since there exists a row in logistic table with the status as 'I'.
It's just like fetching a subset along with all the elements of the set. How would you filter the subset when you are ALSO looking for all the elements of the set?
|
|
|
Re: sql [message #635050 is a reply to message #635043] |
Thu, 19 March 2015 08:40 |
cookiemonster
Messages: 13920 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
What I wrote here:
cookiemonster wrote on Thu, 19 March 2015 11:44Do mean - display all books where the status in the corresponding logistics record has a status of I?
does not appear to be the same as what you wrote here, despite the fact that you apparently agree with me
palpali wrote on Thu, 19 March 2015 12:00Hallo cookiemonster..
yes, i would like to Display all the records if STATUS = I and if STATUS = A then should Display all other records except whose STATUS = A.
thankx
Simply: If you just want records where the status is I then why mention status A at all?
palpali wrote on Thu, 19 March 2015 12:17I dont have my result now, coz' i m try to get my output but i am getting confusion to make a query to get my Output.. sry Michel....
You know what the output should be based on the data you have (if not then you have no idea what you're trying to do and we can't possibly help you). So just write out what the output should be and show us, then we might be able to tell you what query you use to get it.
Also you need to specify what the relationship is between the tables.
|
|
|
|
|
|