tags:

views:

56

answers:

6

I have a database that's setup in the following way

id  coach   team                event              status
14  test    8,7,12,13,15        4,1,2,14,4         2      
13  test    8,12,13,14,15,16    1,2,8,16,16        3  

What i need to do, is search for rows where the first number in the "event" column matches the query. They are separated by commas, but it can be 1 or 2 or 3 digits. Im sure this is possible, just not sure how or where to begin.

+1  A: 
$sql = "SELECT * FROM table_name WHERE event LIKE '" . $query . "',%'";
Jan Hančič
+2  A: 

This will select all rows where the first number in event is 1:

SELECT * FROM `tableName` WHERE event LIKE '1,%';
Inspire
@martin Indeed, I have corrected my example to fix this issue
Inspire
Whats with the downvote? This is the correct answer to his question, and the LIKE clause will use an index on the event column if available in this case.
Inspire
+2  A: 

You could use SUBSTRING_INDEX to get the first value, something like this:

SELECT * FROM table_name WHERE SUBSTRING_INDEX( event, ',', 1 ) = 'value'

With this approach you can use a prepared statement with a placeholder for the search value. Also works fine if there is just one number in the event column, i.e. no commas present to match against.

martin clayton
+2  A: 

You'd be better of by changing your database scheme. Storing fields with lists of ids is not very handy.

Make extra tabels to make the links. For example:

coach_team
id coach_id team_id
1  14       7
2  14       8
3  14       12
4  14       13

Than you can use queries like:

SELECT * FROM table_name WHERE id in
    (SELECT coach_id FROM coach_team WHERE team_id = 1)

(This of course also applies to events.

Extra information: http://en.wikipedia.org/wiki/Database_normalization

Jimmy Shelter
It depends on the usage scenario. For his needs it may be more efficient to keep it as is and avoid the subquery. The LIKE operation in his case will also still use an index if available for the column.
Inspire
That is until he wants another event id than the first one.
Jimmy Shelter
I think Jimmy is right, the information is being stored badly. While storing serialised objects in SQL DB fields can be pragmatic, storing simple comma separated numerals like this is just nonsense, sorry - it's really crying out to be normalised logically.
Iain Collins
+3  A: 

Have you considered normalizing your database? Isn't it a pain to work with a database, in which a field may contain an arbitrary number of arbitrarily formatted values? As a side effect (haha), it will solve the problem you've described in your question.

Example database schema:

create table whatever (
    id int not null auto_increment primary key,
    coach varchar(64),
    status int
)

create table teams (
    id int not null auto_increment primary key,
    name varchar(255)
)

create table events (
   id int not null auto_increment primary key,
   name varchar(255)
)

create table whatever_teams (
    id int not null auto_increment primary key,
    whatever_id int,
    team_id int
)

create table whatever_events (
    id int not null auto_increment primary key,
    whatever_id int,
    event_id int
)

I want to apologize in advance for the obvious lack of sql-injection-enabled code, that can be always found in the questions and answers under the tags "php" and "mysql".

shylent
+1  A: 

I strongly recommend you to change your database schema because from my experience, sooner or later, you have to change it to serve all your needs in the future. SHould do it now be4 too late

vodkhang