|
-
February 28th, 2007, 02:58 AM
#1
How to write sql statement for this kind of condition?
i have a table that keep record of all available items and another table to keep record of used items.
i need to write statement that display unused item.
may i know how to write the sql statement for this condition?
thanks for any help, i really appreciate it very much.
-
February 28th, 2007, 04:31 PM
#2
Re: How to write sql statement for this kind of condition?
what is the structure of your database ?
table names, columns, etc.
-
February 28th, 2007, 08:41 PM
#3
Re: How to write sql statement for this kind of condition?
my tablename is benefits, benefitusage.
the columns that involve are
benefit table:
benefitid
benifitname
benefitusage table:
benefitid
benefitname
So i want to display items from table benefit where the ID does not apprear in benefitusage.
thanks a lot for your help.
-
March 1st, 2007, 06:06 AM
#4
Re: How to write sql statement for this kind of condition?
Try this one
select * from beniftis where benfitid not in (select * from benifitusage)
......or somthing like this
-
March 2nd, 2007, 02:51 AM
#5
Re: How to write sql statement for this kind of condition?
[MSSQL]You can have a single table by adding a boolean column in the Benefit Table
Code:
benefit
---------
benefitid
benifitname
benefitUsage
Then get the unused
Code:
SELECT BenefitName FROM benefit
WHERE BenefitUsage=0
-
March 5th, 2007, 06:09 AM
#6
Re: How to write sql statement for this kind of condition?
Anishkan's answer is the most sensible; Your database design is flawed.. If an item is usable or not, it should be present in only one table and a flag indicate whether it is used or not.
At the moment you are essentially storing everything upt-to twice, needlessly!
Typically, to find everything in one table that does not exist in another, we perform a LEFT join from the solid table to the fragile table, and apply a WHERE filter of fragile.pk IS NULL
If you dont know about left joins, I can provide some links
Last edited by cjard; March 5th, 2007 at 06:14 AM.
-
March 5th, 2007, 06:12 AM
#7
Re: How to write sql statement for this kind of condition?
 Originally Posted by kamran123
Try this one
select * from beniftis where benfitid not in (select * from benifitusage)
......or somthing like this
No no no no no... Never ever ever ever use the IN keyword for a selection list longer than what youre prepared to type by hand (less than 100 items)
Additionally, this wont work because the nested subquery returns an incorrect number of columns.
Please, read some articles on how to write SQL that performs well, not just rubbish that gets the job done slowly, with high resource usage
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|