CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2007
    Posts
    9

    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.

  2. #2
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: How to write sql statement for this kind of condition?

    what is the structure of your database ?
    table names, columns, etc.
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  3. #3
    Join Date
    Feb 2007
    Posts
    9

    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.

  4. #4
    Join Date
    Mar 2006
    Posts
    20

    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

  5. #5
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    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

  6. #6
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    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.
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  7. #7
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: How to write sql statement for this kind of condition?

    Quote 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
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured