Results 1 to 4 of 4

Thread: SQL COUNT Issues

  1. #1
    Hmmm bed
    Join Date
    Jul 2003
    Posts
    441
    Thanks
    5
    Thanked
    0 times in 0 posts

    SQL COUNT Issues

    Hi guys,

    I am writing some SQL to query a database to see if a username already exists, what I have so far is the following:

    Code:

    SELECT COUNT(login) as Expr1 FROM tblEmployee WHERE login=@login



    Now, this kind of works, for example if I enter a random username it will inform me that that is fine and I can carry on, and if I insert a username I know exists such as 'james' it tells me that already exists, so great.

    But if I enter 'jamesjames' into the system, it also tells me that this exusts, when it doesn't!

    Anyone any ideas on why this could be?

    Kind Regards,

    Basher

  2. #2
    Senior Member
    Join Date
    Sep 2004
    Posts
    371
    Thanks
    44
    Thanked
    10 times in 9 posts
    Surely if you just used the UNIQUE keyword in your table definition for the login attribute then you wouldn't need to check if a username already exists?

    Also, you could use regex so putting ^ at the start of the search string and $ at the end to signify the beginning and end of the string.

  3. #3
    cat /dev/null streetster's Avatar
    Join Date
    Jul 2003
    Location
    London
    Posts
    4,138
    Thanks
    119
    Thanked
    100 times in 82 posts
    • streetster's system
      • Motherboard:
      • Asus P7P55D-E
      • CPU:
      • Intel i5 750 2.67 @ 4.0Ghz
      • Memory:
      • 4GB Corsair XMS DDR3
      • Storage:
      • 2x1TB Drives [RAID0]
      • Graphics card(s):
      • 2xSapphire HD 4870 512MB CrossFireX
      • PSU:
      • Corsair HX520W
      • Case:
      • Coolermaster Black Widow
      • Operating System:
      • Windows 7 x64
      • Monitor(s):
      • DELL U2311
      • Internet:
      • Virgin 50Mb
    dont you want the login id to be the primary key?

    or cant you do a "select login from tblEmployee where login=$loginnamehere"

    and if it returns null or "", or whatever sql returns when theres no results.. then you can add the user?
    Last edited by streetster; 27-01-2007 at 08:49 PM.

  4. #4
    Senior Member
    Join Date
    Aug 2005
    Location
    Brisbane
    Posts
    612
    Thanks
    20
    Thanked
    35 times in 22 posts
    • toolsong's system
      • Motherboard:
      • ASUS P7P55D EVO
      • CPU:
      • i7 860 @ 3.8 GHz
      • Memory:
      • 2 x 4GB Corsair Vengeance
      • Storage:
      • SSDs
      • Graphics card(s):
      • GTX970
      • PSU:
      • Seasonic X650
      • Case:
      • Antec P180
      • Operating System:
      • Win7 x64
      • Monitor(s):
      • Dell U2515H
      • Internet:
      • Fibre @ 100/40
    Quote Originally Posted by Basher View Post
    Hi guys,
    Code:

    SELECT COUNT(login) as Expr1 FROM tblEmployee WHERE login=@login

    But if I enter 'jamesjames' into the system, it also tells me that this exusts, when it doesn't!

    'jamesjames' may not exist but you are executing a function "COUNT(login)" as opposed to a straight row SELECT, so there will always be one row returned by the query containing a number, being "0" in this case.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Get 3 issues of Men's Fitness for just £1
    By 0iD in forum Retail Therapy and Bargains
    Replies: 1
    Last Post: 03-11-2006, 06:00 PM
  2. Asus A8N-SLI Deluxe Issues
    By Lee H in forum PC Hardware and Components
    Replies: 6
    Last Post: 24-05-2006, 07:50 PM
  3. SQL Server Connection string?
    By Stoo in forum Software
    Replies: 7
    Last Post: 04-05-2005, 05:48 PM
  4. Shuttle ST61G4 - Stability Issues.
    By DJWillis in forum PC Hardware and Components
    Replies: 4
    Last Post: 23-01-2005, 07:56 PM
  5. Wanted - Custom PC back issues
    By sirelmo in forum Retail Therapy and Bargains
    Replies: 2
    Last Post: 28-07-2004, 08:30 PM

Posting Permissions

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