tags:

views:

64

answers:

4

I have a MySQL table that looks like this:

Table: Designer
  id: integer  
  name: String
  gallery: string

The gallery row can contain a string value like 23,36,45

Now I want to do a query similar to this:

SELECT * FROM Designer WHERE gallery = '36'

I know I kan use LIKE, but that is not precices enough. That could return both 36 and 136.

I also know I could create another table which links designer and gallery. But since this is not going to be a huge table, I'm adding the foreign gallery ID Key to the gallery row. And I'm lazy right now ;)

So how can I select a row that has the number 36?

UPDATE
Ok, since I'm getting nailed for poor design (yes I know it was), I See the obvious now.

A designer can have many galleries, but a gallery can only belong to one designer. Therefore I only need to add designer ID as a foreign key to the gallery table.

Simple. But not always logical when it's 3AM and you've been workign for 15 hours ;)

+3  A: 

If you have to do that you have poorly designed your tables.

One designer can have got many galleries and a gallery belong to one designer means you must create a foreign key 'designer' in your 'gallery' table, and your request will be

SELECT *
FROM Designer
INNER JOIN Gallery
ON Gallery.id = 36
AND Designer.id = Gallery.designer
MatTheCat
No, the gallery can only belong to one designer. But a designer can hve many galleries. See my update.
Steven
I've edited my post.
MatTheCat
I think it's an overkill to use INNER JOIN here. Much easier just to do `SELECT * FROM Designer, gallery WHERE designer.id = gallery.fk_designer_id`.
Steven
It's about join syntax, mine is recommended http://dev.mysql.com/doc/refman/5.1/en/join.html otherwise this is the same query.
MatTheCat
+1  A: 

You really shouldn't store your data like that since it makes queries horribly inefficient. For that particular use case, you can (if you don't care about performance) use:

select * from designer
    where gallery like '36,%'
       or gallery like '%,36'
       or gallery like '%,36,%'
       or gallery = '36'

This will alleviate your concerns about partial matches since something like 136 will not match any of those conditions but 36 in any position will match.


However, despite your protestations to the contrary, what you should do is re-engineer your schema, something like:

designer:
    id             integer primary key
    name           varchar(whatever)
designer_galeries:
    designer_id    integer foreign key references designer(id)
    gallery        string
    primary key    (designer_id,gallery)

Then your queries will be blindingly fast. One of the first things you should loearn is to always design your schema for third normal form. You can revert to other forms for performance once you understand the trade-offs (and know how to mitigate problems) but it's rarely necessary unless you database is horribly convoluted.

paxdiablo
I'm pretty competent i DB design and I **know** this was bad design. But I was to tired to think (and didn't use pen and paper when designing). So yes, adding foreign key in gallery table is by far the best solution!
Steven
@Steven, you _can_ use the first solution if you don't care that much about performance. For example, it's perfectly acceptable if you know your table will never exceed 100 rows and won't ever be hit with more than a few concurrent queries since you probably don't _need_ professional DBMS performance in that case. That's YAGNI kicking in. It's just that, in my experience, tables never stay as small as you expected :-)
paxdiablo
he,he - you are right. This is for displaying fashion designers on a website. Although I have around 60 designers, many ppl are opening the page to view galleries and brows designer info. During off season, I'll maybe have 10-20 a week. But during fashion week, I might have 2.000 a day :)
Steven
A: 

You can use regular expressions in your WHERE clause instead.

http://dev.mysql.com/doc/refman/5.1/en/regexp.html

SELECT * FROM Designer WHERE gallery REGEXP "(,|^)(36)(,|$)"
madsleejensen
+1  A: 

I also agree that this is poorly designed table structure but here is the answer

SELECT * FROM Designer where FIND_IN_SET(36,gallery)
gajendra.bang
This worked like a charm. Thanks.
Steven
Someoe down voted the answer, if the answer works, please vote it up :)
gajendra.bang
I have upvoted the answer :)
Steven
Thanks a lot :)
gajendra.bang