Submit Your Article Webforumz RegistrationAnnouncements Contact Webforumz StaffContact
Home Resources Blogs Meet the Team Contact Register
 

Go Back   WebForumz.com > Putting it Together > Databases

Reply
 
LinkBack Thread Tools
Old July 22nd, 2008, 06:51 AM   #1
New Member
 

Join Date: Jul 2007
Location: England
Age: 26
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 Xyis is on a distinguished road
SQL Conditions?

Hi there,

I have this SQL

SELECT tblUserData.*
FROM tblUserData
ORDER BY rank DESC

and i would like to pass into it a gender, for example.

SELECT tblUserData.*
FROM tblUserData
WHERE sex = @Gender
ORDER BY rank DESC

The trouble is @Gender can equal one of three things, "male", "female" or "both". This works fine for male and female but how can i get it to return both male AND female if i am passing in "both"?

Thanks,
Xyis.
Xyis is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old July 22nd, 2008, 07:04 AM   #2
Reputable Member
 

Join Date: Jul 2008
Location: UK and Spain
Age: 26
Posts: 114
Thanks: 2
Thanked 15 Times in 14 Posts
Rep Altering Power: 0 stu2000 is on a distinguished road
Re: SQL Conditions?

you could use an if statement,

Code:
if @Gender == 'both'
query = SELECT tblUserData.* FROM tblUserData ORDER BY rank DESC
else
query = SELECT tblUserData.* FROM tblUserData WHERE sex = @Gender ORDER BY rank DESC
or something along those lines, depends what code (PHP, ASP etc) you are using on your site as to exactly how you code it into the page.

would that work for what you want to do?
stu2000 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old July 23rd, 2008, 05:41 AM   #3
New Member
 

Join Date: Jul 2007
Location: England
Age: 26
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 Xyis is on a distinguished road
Re: SQL Conditions?

Sorry mate, this aint working for me. Says it's "unable to parse query text". I tried to correct the error myself but my knowledge of SQL is limited, hence me being here .

I am using an ASP.NET typed dataset to make a table adapter method GetPeople(@Gender) which gets all the people in the database based off the value of Gender (male, female, both)
Xyis is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old July 23rd, 2008, 05:57 AM   #4
Elite Veteran
SuperMember
 
welshstew's Avatar
 

Join Date: May 2007
Location: inside the outside
Posts: 3,909
Blog Entries: 14
Thanks: 9
Thanked 40 Times in 38 Posts
Rep Altering Power: 0 welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute welshstew has a reputation beyond repute
Re: SQL Conditions?

Try the following:
Code:
SELECT tblUserData.* FROM tblUserData WHERE
(Gender='Male' OR Gender='Female' OR Gender='Both')
ORDER BY rank DESC


__________________
WelshStew
extreme sports clothing and t-shirts | twitter| web design
welshstew is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old July 23rd, 2008, 06:10 AM   #5
New Member
 

Join Date: Jul 2007
Location: England
Age: 26
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 Xyis is on a distinguished road
Re: SQL Conditions?

Thanks for the reply, I don't need to select one of 3 values, i need to select data based on the value of @Gender.

If @gender = 'male' I want to select all the male people.
If @gender = 'female' I want to select all the female people.
and If @gender = 'both' I want to select all the people (male and female)

I'm starting to think about just having two separate queries and getting my code to work out which one to use.
Xyis is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old July 23rd, 2008, 07:05 AM   #6
New Member
 

Join Date: Jul 2007
Location: England
Age: 26
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 Xyis is on a distinguished road
Re: SQL Conditions?

Used a stored procedure based off the original reply, is now working.

Thanks.

(@Gender NVARCHAR(7))

AS
if @Gender = 'both'
SELECT tblUserData.* FROM tblUserData ORDER BY rank DESC
else
SELECT tblUserData.* FROM tblUserData WHERE sex = @Gender ORDER BY rank DESC
GO
Xyis is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old July 23rd, 2008, 12:53 PM   #7
Reputable Member
 

Join Date: Jul 2008
Location: UK and Spain
Age: 26
Posts: 114
Thanks: 2
Thanked 15 Times in 14 Posts
Rep Altering Power: 0 stu2000 is on a distinguished road
Re: SQL Conditions?

no problems, sorry I don't code ASP so wouldn't have been able to give you the exact ASP code, but glad my idea made sense... kind of lol!
stu2000 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Validate a form only if certain conditions are met svennie12 JavaScript 2 May 10th, 2008 10:59 AM
terms and conditions goldy The Café 10 January 16th, 2008 05:57 PM
Select records based on multiple conditions - need help Love2Java Databases 9 October 29th, 2007 11:37 AM
Terms and Conditions gsingh Legal Discussion 4 August 17th, 2007 04:13 AM
Use frames as conditions? Throntel Flash and ActionScript 0 January 13th, 2007 06:50 PM


Search Engine Optimization by vBSEO 3.2.0 RC8