www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Server-Side Development > SQL

    SQL For all Structured Query Language, and general database questions.

    Reply
     
    Thread Tools Rate Thread Display Modes
      #1  
    Old 09-25-2007, 02:27 AM
    Mr Initial Man's Avatar
    Mr Initial Man Mr Initial Man is offline
    Registered User
     
    Join Date: Sep 2004
    Location: At the corner of WALK and DONT WALK
    Posts: 1,519
    Using SQL to count occurrences of substring

    SQL Code so far:
    Code:
    SELECT charname "Character", Status, Description, count(comics.Characters) "Appearances"
    FROM characters
    JOIN comics ON Characters.CharName LIKE comics.Characters
    GROUP BY charname;
    Current Results:
    If character.CharName = 'Sam', only instances where comics.Characters = 'Sam' are counted.

    Needed Results:
    If character.CharName = 'Sam', all instances where comics.Characters has the substring 'Sam' are counted. (i.e. if comics.Characters = 'Bud Sam' or comics.Characters = 'Jeff Sam', etc.)

    Possible Issue
    Would it be easier simply to create two seperate queries in the PHP page this statement will be used in: One to list character names, descriptions, and status; the other to count the number of appearances?
    __________________
    Coach Random Comic

    Last edited by Mr Initial Man; 09-25-2007 at 02:36 AM.
    Reply With Quote
      #2  
    Old 09-25-2007, 02:43 AM
    mojtaba mojtaba is offline
    Registered User
     
    Join Date: Aug 2007
    Location: Shiraz
    Posts: 9
    Hi,
    Try using % sign before and after comics.Characters ... But remember it you must concatenation the string ('%') and Filed name ( comics.Characters)

    Good luck,
    Bye.
    Reply With Quote
      #3  
    Old 09-25-2007, 04:55 AM
    Mr Initial Man's Avatar
    Mr Initial Man Mr Initial Man is offline
    Registered User
     
    Join Date: Sep 2004
    Location: At the corner of WALK and DONT WALK
    Posts: 1,519
    This, I tried, with no luck. Here are the two versions I used:

    Version 1:
    Code:
    SELECT charname "Character", status "Status", count(comics.Characters) "Appearances"
    FROM characters
    JOIN comics ON Characters.CharName LIKE concat("%",concat(comics.Characters,"%"))
    GROUP BY charname;
    Result:
    Code:
    +------------+-----------+-------------+
    | Character  | Status    | Appearances |
    +------------+-----------+-------------+
    | Bud        | Regular   |          16 | 
    | Dad        | Recurring |           1 | 
    | Dorkboy    | Recurring |           5 | 
    | Glen       | Recurring |           6 | 
    | Jeff       | Regular   |          22 | 
    | Johnny     | Recurring |           3 | 
    | Nightlatch | Recurring |           6 | 
    | Sam        | Regular   |          13 | 
    +------------+-----------+-------------+
    8 rows in set (0.02 sec)
    Again, this simply returns how many "solo" appearances each has.

    Version 2:
    Code:
    SELECT charname "Character", status "Status", count(comics.Characters) "Appearances"
    FROM characters
    JOIN comics ON Characters.CharName LIKE "%"||comics.Characters||"%"
    GROUP BY charname;
    Code:
    +------------+-----------+-------------+
    | Character  | Status    | Appearances |
    +------------+-----------+-------------+
    | Bud        | Regular   |         310 | 
    | Dad        | Recurring |         310 | 
    | Dorkboy    | Recurring |         310 | 
    | Glen       | Recurring |         310 | 
    | Jeff       | Regular   |         310 | 
    | Johnny     | Recurring |         310 | 
    | Nightlatch | Recurring |         310 | 
    | Sam        | Regular   |         310 | 
    +------------+-----------+-------------+
    8 rows in set, 1 warning (0.02 sec)
    This returns how many strips have appearances by any cast member, rather than the specific cast member.
    __________________
    Coach Random Comic
    Reply With Quote
      #4  
    Old 09-25-2007, 11:04 AM
    mattyblah mattyblah is offline
    Registered User
     
    Join Date: Jul 2004
    Posts: 300
    what db are you using? sql server or mysql? something else?
    Reply With Quote
      #5  
    Old 09-25-2007, 11:33 AM
    Mr Initial Man's Avatar
    Mr Initial Man Mr Initial Man is offline
    Registered User
     
    Join Date: Sep 2004
    Location: At the corner of WALK and DONT WALK
    Posts: 1,519
    mysql
    __________________
    Coach Random Comic
    Reply With Quote
    Reply

    Bookmarks


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

    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
    Forum Jump


    All times are GMT -5. The time now is 01:16 PM.



    Acceptable Use Policy

    Internet.com
    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.