|
|||||||
| SQL For all Structured Query Language, and general database questions. |
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
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; 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. |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
||||
|
||||
|
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;
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) 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)
__________________
Coach Random Comic |
|
#4
|
|||
|
|||
|
what db are you using? sql server or mysql? something else?
|
|
#5
|
||||
|
||||
|
mysql
__________________
Coach Random Comic |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|