| |
|
If this is your first visit, be sure to check out the FAQ by clicking the link above.
You may have to register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
|
 |

01-17-05, 20:15
|
|
Registered User
|
|
Join Date: Aug 2003
Location: san jose, CA
Posts: 68
|
|
|
using wildcard character in select statement
|
|
Dear forums members:
I am doing a SQL select statement exercise. the table is books which has ISBN, title, pubdate, pubid, category columns in it.
The question ask me to select all books that are in children and cooking category. (using wildcard characters % and _ are suggested for this problem, no logical operators: and, or, not are allowed in this exercise)
I tried the following way to generate query.
a)select * from books
where category like 'C%N'; --> only catched books in children category
b) select * from books
where category like '%NG'; --> only catched books in cooking category
I am kind of out of ideas here. Please give me some suggestion on how to use combination of wildcard characters to complete this problem. Thanks!
sjgrad03
1-17-05
|
|

01-17-05, 21:44
|
|
Resident Curmudgeon
|
|
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
|
|
Is this problem from a book, a class, or something different? I suspect that some important pieces are missing from your problem specification, and I'm wondering if I can find the original problem specification either online or published in a book somewhere.
-PatP
|
|

01-17-05, 21:49
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
|
|
|
|
yeah, homework, but a couple of valiant attempts, so ...
... where category like 'c%i%n%'
neat, eh? 
|
|

01-17-05, 23:55
|
|
Resident Curmudgeon
|
|
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
|
|
What if Chicken is a category, or if the categories can be in any order? I've seen a spec something like this a couple of years ago for a university level class, but I can't remember them very well.
That's what happens when you get old, like me... You start loosing details that you don't use!
-PatP
|
|

01-18-05, 00:18
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
|
|
losing, not loosing
actually, like 'c%i%n%' will find chicken as well
categories in any order? how about one per row? 
|
|

01-18-05, 08:26
|
|
Resident Curmudgeon
|
|
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
|
|
One category per row would be kinda-sorta normalized. That would make things easy!
The problem spec that I saw was a comma delimited list, where you had to construct a wildcard pattern to cope with the list. The problem was part of a suite of related problems, and it was intended for very early in a university class, as a springboard to the concept of first normal form.
The series of problems was really well designed, and helped tie the course together from a practical/real world perspective. The problems helped the students understand the real world implications of good database design really well. The guy that wrote the curriculum was named Keith, but I can't remember his last name or University, although I think he was either extreme NorthEast or MidWest United States. I'll have to see if I can coax my feeble gray cells into coughing up more contact information.
-PatP
|
|

01-18-05, 09:44
|
|
Registered User
|
|
Join Date: Aug 2004
Posts: 330
|
|
How about this:
select * from books
It will bring back all results, therfore satisfying the requrement "all books that are in children and cooking category". You will also get books that are not in the children and cooking category, but you may get that anyway if you use the % and _.
|
|

01-18-05, 09:58
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
|
|
urquel, brilliant!!
if i'm marking the test, you get full marks for that answer
|
|

01-18-05, 10:58
|
|
Jaded Developer
|
|
Join Date: Nov 2004
Location: out on a limb
Posts: 9,246
|
|
I suppose it depends on the precise wording of the question, or beeing pedantic the precise interpreation you have put on the question
is it ALL childrens books AND ALL cooking books or is it all Childrens Cooking Books
as ORs are excluded
wonder if the SQL engine will support POSIX style regular expressions?
|
|

01-18-05, 15:05
|
|
Registered User
|
|
Join Date: Aug 2004
Posts: 330
|
|
Don't forget "Cooking Children" category.... you would get one hit.... Hansel and Gretel
|
|

01-18-05, 21:57
|
|
Registered User
|
|
Join Date: Aug 2003
Location: san jose, CA
Posts: 68
|
|
|
thanks for the help!
Hello PatP, urquel, r937:
Thanks for the replying. I solved problem by using union operation for this problem.
The original problem states: use a search pattern to list all books in the children and cooking categories. Do not use any logical operators in the where clause.
My solution is: select * from books
where category like '%N'
union
select * from books
where category like '%G'
This union operation wil list all the books in children and cooking category for this exercise.
P.S The books I am using is "Oracle 9i: SQL with an introduction to PL/SQL"
sincerely,
sjgrad03
1-18-05
|
|

01-18-05, 23:26
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
|
|
um, i would argue that UNION is a logical operator
|
|

01-19-05, 11:41
|
|
Registered User
|
|
Join Date: Aug 2004
Posts: 330
|
|
This %N OR(Logical equivalent to UNION) %G solution is NON DETERMINISTIC. In other words. You will get rows back for "CHILDREN" and for "COOKING" but also for "MEN", "WOMEN" and "GARDENING" and no results for "Children" or "Cooking" (Assuming that your DBMS is case-sensetive). In other words, you are better off using SELECT * FROM BOOKS; At least you know that all rows will be returned. Maybe if you knew the range and domain of the CATEGORY column, you could better formulate an answer.
|
|

01-19-05, 11:59
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
|
|
well, that's why i suggested LIKE 'c%i%n%'
it's less non-deterministic
i.e. better 
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|