Click to See Complete Forum and Search --> : How to write sql statement for this kind of condition?
engloon
February 28th, 2007, 01:58 AM
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.
hspc
February 28th, 2007, 03:31 PM
what is the structure of your database ?
table names, columns, etc.
engloon
February 28th, 2007, 07:41 PM
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.
kamran123
March 1st, 2007, 05:06 AM
Try this one
select * from beniftis where benfitid not in (select * from benifitusage)
......or somthing like this
aniskhan
March 2nd, 2007, 01:51 AM
[MSSQL]You can have a single table by adding a boolean column in the Benefit Tablebenefit
---------
benefitid
benifitname
benefitUsageThen get the unusedSELECT BenefitName FROM benefit
WHERE BenefitUsage=0
cjard
March 5th, 2007, 05:09 AM
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
cjard
March 5th, 2007, 05:12 AM
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
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.