473,486 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SELECT From a field separated by a comma.

9 New Member
Hi there, I am not sure if this can be done entirely in SQL. Anyhow, I have the following 4 tables.

TBL_Practice
Practice_ID (PK)
Practice_Name
etc.

TBL_Branch
Branch_ID (PK)
Practice_ID (FK)
Branch_Suburb
Branch_Postcode
etc.

Each branch has multiple specialties that they can practice in

TBL_SPECIALTY
Specialty_ID (PK)
Specialty_Name (eg. family law)
Specialty_Tags (eg. contains a list of tags: marriage, divorce, custody)

Then there is a mapping table as such.

TBL_BRANCH_SPECIALTY_MAP
Branch_ID (FK)
Specialty_ID(FK)

Thats the database section, then on the frontend I then have a search engine with 3 textboxes.

[Practice Name or Part thereof]
[Practice Suburb or Postcode]
[Keywords]

If someone enters a keyword that appears in TBL_SPECIALTY.Specialty_Tags it should bring up a matching practice.

I have the first two boxes worked out but cannot figure out how to get the keywords working. (or if it can be done).

My SQL so far is:

Expand|Select|Wrap|Line Numbers
  1. SELECT  TBL_PRACTICE.Practice_Name, TBL_BRANCH.Branch_Suburb, TBL_BRANCH.Branch_Postcode
  2. FROM TBL_PRACTICE INNER JOIN
  3. TBL_BRANCH ON TBL_PRACTICE.Practice_ID = TBL_BRANCH.Practice_ID 
  4. WHERE     
  5. TBL_PRACTICE.Practice_Name LIKE '% PARAMETER_PRACTICE %' AND (Suburb_Name LIKE '% PARAMETER_LOCATION %' OR Suburb_Postcode = ' PARAMETER_LOCATION ') 
  6.  
Thanks in advance for any assistance, much appriciated.
Oct 29 '09 #1
2 3109
Atli
5,058 Recognized Expert Expert
Hey.

This problem is only a problem because your TBL_SPECIALITY.Specialty_Tags field violates the first rule of relational databases: Each field should only contain a single piece of data. (See this article for a more detailed explanation.)

MySQL has no way of knowing that the string in that field is meant to be a list of keywords, so none of the typical search methods can be applied to it. You could try to manually search the string, but that's usually a performance killer.

What you should do, is remove the field entirely and set up a table to store a list of available tags, and a table that links tags to specialties.
Expand|Select|Wrap|Line Numbers
  1. tbl_speciality
  2. specialty_id (PK)
  3. specialty_name (eg. family law)
  4.  
  5. tbl_tag
  6. tag_id (PK)
  7. tag_name (eg. marriage, divorce, custody)
  8.  
  9. tbl_speciality_tag
  10. specialty_id (PK, FK)
  11. tag_id (PK, FK)
And then you could do a query like:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     p.practice_name, 
  3.     b.branch_suburb, 
  4.     b.branch_postcode
  5. FROM tbl_practice AS p
  6. INNER JOIN tbl_branch AS b
  7.     ON p.practice_id = b.Practice_id
  8. INNER JOIN tbl_branch_speciality_map AS sm
  9.     ON sm.branch_id = b.branch_id
  10. LEFT JOIN tbl_speciality AS s
  11.     ON sm.speciality_id = s.speciality_id
  12. WHERE     
  13.     p.practice_name LIKE '% PARAMETER_PRACTICE %' 
  14. AND(    b.suburb_name LIKE '% PARAMETER_LOCATION %' 
  15.     OR  b.suburb_postcode = ' PARAMETER_LOCATION ') 
  16. AND
  17.     s.speciality_id IN(
  18.         SELECT st.speciality_id
  19.         FROM tbl_speciality_tag AS st
  20.         INNER JOIN tbl_tag AS t
  21.             ON t.tag_id = st.tag_id
  22.         WHERE 
  23.             t.tag_name IN('Tag1', 'Tag2')
  24.     )
Note, the query is just an example. There might be a bug or two in there I can't spot atm.

Also,
I recommend you reconsider your naming conventions.
The style I used in my examples, all lower-case using "_" as a word-separator, is a very good one.

Generally, in SQL databases, it is best to reserve all upper-case words for SQL commands, and use all lower-case letters in table and field names. It makes it easier to distinguish between SQL syntax and names, and avoids the case-confusion created by Windows based servers.
(Windows is case-insensitive, while Unix based systems are case-sensitive, so Windows doesn't realize if you use the wrong case, while Unix does.)

Ultimately, it is most important to be consistent, to choose a style and use it exclusively throughout the project.
Oct 31 '09 #2
new2sql
9 New Member
Atli,

Thanks for your advice on this, I had iniitally thought that another table would be the way to go but was not 100% sure. After reading your post I will implement the design as per your recommendations and the recommendations in the normalisation link you attached.

Much appriciated.
Nov 1 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
9881
by: Greg Bryant | last post by:
Hi folks. I'm porting a cf site to php, everything's going very well, I like php much better (this, of course, being the correct forum to make that statement :). One problem I have is with...
3
3308
by: Funnyweb | last post by:
I have a database table, which has field that could contain a single integer or a list of comma separated integers. Is it possible to match each row of that field against an array of integers...
5
2525
by: Mark | last post by:
Hi - I have set-up security for my users - the security is held in a text field, separated by a comma. If the users a member of groups 1, 5 and 6 - the usergroups field is set to 1,5,6 - to...
4
2111
by: Mick White | last post by:
mysql> select * from guestbook; +----+--------+---------+-----------------+----------------+ | id | fname | lname | comments | time_in |...
1
2273
by: Mark | last post by:
How can I use the selections in a multi-select listbox as criteria in a query? Thanks! Mark
1
2010
by: jamminc | last post by:
Hi All, I am currently developing a module where the user will input multiple items (as many as 20-600) and I am suppose to retrieve it from a sql database all the information and update it with...
2
8075
by: karups | last post by:
Hi, I've got listbox in my .aspx page where the users can make multiple selection. So, Users can select any number of items in listbox, I have to take value from items and pass it to stored...
22
12418
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
4
4637
by: sufian | last post by:
Below is the field where user enters his/her email address and the AJAX post request is sent to the server and the user sees the message: echo("<div id=\"message\" class=\"success\">Thank you! You...
2
1437
by: haridharmajan | last post by:
I Need to add multiple date into a field using calender each date is to be separated by comma ie when I click on the show month it must display calender and when I click on a date it must inser it to...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7132
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7180
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7341
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4870
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
600
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
266
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.