tags:

views:

33

answers:

2

I have a table setup the following way

  `id` int(11) NOT NULL AUTO_INCREMENT,
  `eventid` int(11) NOT NULL,
  `invites` text COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)

In that table i have rows which look like this

1, 127, '32215, 55648, 89887, 55555, 66666, 33333'
2, 168, '44444, 33333, 121212, 0540541'

How would i search the contnts of the invites field for a match for a particular number such as if i wanted to search for 33333, it would return both rows, but if i searched for 44444 it would return only the second?

+3  A: 

I believe you can use find_in_set(), so something like:

select * from tablename where find_in_set('44444', invites);

This isn't very good database design though.

catfarm
what are the downsides and what would be a better solution? Take into account that i may have up to 1000 diff numbers per row in the invites field
Patrick
the downside is that find_in_set() will be pretty slow if it has to parse through a text field looking for ids, a better solution would be to just have multiple rows for each eventid and make invites be an int, each row being one of the values of your current invites field. Then add indexes to both eventid and invites (or inviteid if you rename it, i would, assuming those are ids), a simple select on said table for a particular inviteid will return the correct eventids much faster.
catfarm
A: 

As a side note, allow me to note that your database schema - storing a set of values in a plain-text field - is improper. If you want to query on a one-to-many relationship like this, you ought to have another table that defines the relationship between a single event and a single invitation, like this:

eventid invitationid
1 15
1 16
1 17
2 18
3 19
3 20

Then you'll be able to do standard SQL queries that will actually be performant and will make sense.

Alex