Go Back   Cyber Tech Help Support Forums > Software > Web Development & Graphic Design

Notices

Web Development & Graphic Design Problem Solving for Graphic Design, PHP, ASP, Perl, MySQL, SQL, XML, HTML issues

Reply
 
Topic Tools
  #1  
Old December 18th, 2007, 02:08 PM
FrEaKmAn FrEaKmAn is offline
Senior Member
 
Join Date: Aug 2005
Posts: 477
Question mysql if clause maybe?

Hello,

what I want to do is that I'm able to assign for example artist to more then one category. I know I could do this with creating additional table, but for my case 95% artists will belong to only 1 category, while other 5% to more the one and I don't want to fill mysql db for nothing..

I found IN clause but not exactly that I want. If we have a field category and I input category ids like 14,15, can I use some command to separate string into arrays and check them with SQL?

Code:
+---------+--------+----------------------+
| id      | artist | category             |
+---------+--------+----------------------+
| 1       | John   | 14,15                |
+---------+--------+----------------------+
I tried SELECT * FROM artists WHERE category IN (14,15), well it does work and if I search only 14 it works, if 15 then nothing comes out...
Reply With Quote
  #2  
Old December 19th, 2007, 09:14 AM
oracle128's Avatar
oracle128 oracle128 is offline
Senior Member
 
Join Date: Oct 2000
O/S: Windows XP Pro
Location: Melbourne, Australia
Age: 40
Posts: 9,401
It would definitely be worth using a separate table for this, even if only 1 of your artists has more than one category. You should be aware that having multiple values in a single field is a denormalized solution.
It is possible to work with it, but not necessarily desirable.

IN is not meant for what you want. IN is for searching for one of several possible values. IN is essentially a shorthand way of writing multiple OR conditions.
Code:
SELECT * FROM table WHERE value IN (1,2,3)
is equivalent to
Code:
SELECT * FROM table WHERE value=1 OR value=2 OR value=3
Separating comma separate values into string arrays (aka tokenizing) cannot be done in SQL, as it is neither ANSI SQL functionality, nor does any common database implement it. You could implement it in PL/SQL if you wanted. MySQL supports Functions and Procedures.

With plain SQL, you have two base options: LIKE or INSTR. Both solutions are similar:
Code:
SELECT * FROM artists WHERE category LIKE '%14%'
Code:
SELECT * FROM artists WHERE INSTR(category,'14')>0
Both do the same thing, and take about the same amount of time to process: LIKE is desirable because it's ANSI SQL.
But they both have the same flaw: if you have an artist in category '140', searching for category '14' with the above statements will return that artist (because '14' is in the string '140').

The best way to avoid this is to be sure every category is surrounded by delimiters (in this case, commas) on both sides. So instead of '14,15' you have ',14,15,'. This way, you can include the surrounding commas in the search, and be sure you're getting the whole value:
Code:
SELECT * FROM artists WHERE category LIKE '%,14,%'
will return artists in category 14, but not 140.

In fact, you don't even have to store the commas in the database, you can concatenate the strings:
Code:
SELECT * FROM artists WHERE CONCAT(',',category,',') LIKE '%,14,%'
Here's what it would look like if you normalized the database:
Code:
artists
+---------+--------+
| id      | artist |
+---------+--------+
| 1       | John   |
+---------+--------+

categories
+---------+----------------------+
| id      | category             |
+---------+----------------------+
| 1       | sculptor             |
+---------+----------------------+
| 2       | painter              |
+---------+----------------------+

artistcats
+-----------+-------------+
| artistid  | categoryid  |
+-----------+-------------+
| 1         | 1           |
+-----------+-------------+
| 1         | 2           |
+-----------+-------------+
(Primary key in artistcats would be a joint key of artistid and categoryid; or could be a separate Id field)

Last edited by oracle128; December 19th, 2007 at 09:32 AM.
Reply With Quote
Reply

Bookmarks


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

Similar Topics
Topic Topic Starter Forum Replies Last Post
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) bot96 Internet / Browsers 0 August 24th, 2012 07:51 PM
mysql WHERE FrEaKmAn Web Development & Graphic Design 1 February 25th, 2007 01:53 PM
PHP+MySQL : not valid MySQL result resource? Will464 Web Development & Graphic Design 3 March 11th, 2005 08:07 PM
Signs that your wife is messing around with Santa Clause (adult) modmidget Jokes Forum 1 December 7th, 2004 10:51 PM
Web based managment system (Php VS MySQL AND VB vs MYSQL) nasirkw The Anything Else Board 1 May 9th, 2004 03:28 PM


All times are GMT +1. The time now is 09:52 PM.