|
Web Development & Graphic Design Problem Solving for Graphic Design, PHP, ASP, Perl, MySQL, SQL, XML, HTML issues |
|
Topic Tools |
#1
|
|||
|
|||
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 | +---------+--------+----------------------+ |
#2
|
||||
|
||||
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) Code:
SELECT * FROM table WHERE value=1 OR value=2 OR value=3 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 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,%' 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,%' Code:
artists +---------+--------+ | id | artist | +---------+--------+ | 1 | John | +---------+--------+ categories +---------+----------------------+ | id | category | +---------+----------------------+ | 1 | sculptor | +---------+----------------------+ | 2 | painter | +---------+----------------------+ artistcats +-----------+-------------+ | artistid | categoryid | +-----------+-------------+ | 1 | 1 | +-----------+-------------+ | 1 | 2 | +-----------+-------------+ Last edited by oracle128; December 19th, 2007 at 09:32 AM. |
Bookmarks |
«
Previous Topic
|
Next Topic
»
|
|
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.