Computer Webmaster Gaming Console Graphics Forum

Welcome to the Computer Webmaster Gaming Console Graphics Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

MK PitStop Main Earn $25 Earn Money Posting Extras Members Blogs Image Hosting User Pages
Go Back   Computer Webmaster Gaming Console Graphics Forum > Webmaster Forum > Website Coding > Database
Register FAQ/Rules Become A V.I.P. Member Search Today's Posts Mark Forums Read

Database Database problems or need to ask a question? maybe something to do with sql injections or a database software question. Database topics cover MySQL, PostgreSQL, Oracle, SQL Server or anything else related to databases.

Google
Closed Thread
 
LinkBack Thread Tools Display Modes
Old 07-01-2007, 11:01 PM   #1
John Rappold
 
John Rappold's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default How do I parse a comma separated field?

I have a table with the following fields:
tableid (int)
userid (varchar)
lvalues (varchar)

the lvalues column contains comma separted values. e.g. 12, 5, 32
each individual values matches a field in another table.

How do I write a SQL query that parses the comma separated data? For
example:
WHERE user id=30 so that the results look like:

30 | 12
30 | 5
30 |32

TIA


 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Advertisements
Old 07-01-2007, 11:01 PM   #2
Captain Paralytic
 
Captain Paralytic's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default How do I parse a comma separated field?

On 14 Jun, 15:03, "John Rappold" <jrapp...@jscoca-k12.org> wrote:
> I have a table with the following fields:
> tableid (int)
> userid (varchar)
> lvalues (varchar)
>
> the lvalues column contains comma separted values. e.g. 12, 5, 32
> each individual values matches a field in another table.
>
> How do I write a SQL query that parses the comma separated data? For
> example:
> WHERE user id=30 so that the results look like:
>
> 30 | 12
> 30 | 5
> 30 |32
>
> TIA


You don't you build a table structure where each value has its own
record in another table.

From: http://dev.mysql.com/tech-resources/...alization.html
"The first normal form (or 1NF) requires that the values in each
column of a table are atomic. By atomic we mean that there are no sets
of values within a column."

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 07-01-2007, 11:01 PM   #3
John Rappold
 
John Rappold's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default How do I parse a comma separated field?

This is a lookup table and the data is already there in the comma separated
format. I can't change it.

I'm thinking I need to do some sort of SELECT IN and then create a temporary
table.

Can anyone help me?
"Captain Paralytic" <paul_lautman@yahoo.com> wrote in message
news:1181830481.681955.25380@e9g2000prf.googlegrou ps.com...
> On 14 Jun, 15:03, "John Rappold" <jrapp...@jscoca-k12.org> wrote:
>> I have a table with the following fields:
>> tableid (int)
>> userid (varchar)
>> lvalues (varchar)
>>
>> the lvalues column contains comma separted values. e.g. 12, 5, 32
>> each individual values matches a field in another table.
>>
>> How do I write a SQL query that parses the comma separated data? For
>> example:
>> WHERE user id=30 so that the results look like:
>>
>> 30 | 12
>> 30 | 5
>> 30 |32
>>
>> TIA

>
> You don't you build a table structure where each value has its own
> record in another table.
>
> From:
> http://dev.mysql.com/tech-resources/...alization.html
> "The first normal form (or 1NF) requires that the values in each
> column of a table are atomic. By atomic we mean that there are no sets
> of values within a column."
>



 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 07-01-2007, 11:01 PM   #4
lark
 
lark's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default How do I parse a comma separated field?

== Quote from John Rappold (jrappold@jscoca-k12.org)'s article
> This is a lookup table and the data is already there in the comma separated
> format. I can't change it.
> I'm thinking I need to do some sort of SELECT IN and then create a temporary
> table.
> Can anyone help me?
> "Captain Paralytic" <paul_lautman@yahoo.com> wrote in message
> news:1181830481.681955.25380@e9g2000prf.googlegrou ps.com...
> > On 14 Jun, 15:03, "John Rappold" <jrapp...@jscoca-k12.org> wrote:
> >> I have a table with the following fields:
> >> tableid (int)
> >> userid (varchar)
> >> lvalues (varchar)
> >>
> >> the lvalues column contains comma separted values. e.g. 12, 5, 32
> >> each individual values matches a field in another table.
> >>
> >> How do I write a SQL query that parses the comma separated data? For
> >> example:
> >> WHERE user id=30 so that the results look like:
> >>
> >> 30 | 12
> >> 30 | 5
> >> 30 |32
> >>
> >> TIA

> >
> > You don't you build a table structure where each value has its own
> > record in another table.
> >
> > From:
> > http://dev.mysql.com/tech-resources/...alization.html
> > "The first normal form (or 1NF) requires that the values in each
> > column of a table are atomic. By atomic we mean that there are no sets
> > of values within a column."
> >


you can use a function called substring_index to separate the values.
--
POST BY: lark with PHP News Reader
 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 07-01-2007, 11:02 PM   #5
anokun7
 
anokun7's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default How do I parse a comma separated field?

On Jun 14, 1:41 pm, lark <ham...@sbcglobal.net> wrote:
> == Quote from John Rappold (jrapp...@jscoca-k12.org)'s article
>
>
>
> > This is a lookup table and the data is already there in the comma separated
> > format. I can't change it.
> > I'm thinking I need to do some sort of SELECT IN and then create a temporary
> > table.
> > Can anyone help me?
> > "Captain Paralytic" <paul_laut...@yahoo.com> wrote in message
> >news:1181830481.681955.25380@e9g2000prf.googlegro ups.com...
> > > On 14 Jun, 15:03, "John Rappold" <jrapp...@jscoca-k12.org> wrote:
> > >> I have a table with the following fields:
> > >> tableid (int)
> > >> userid (varchar)
> > >> lvalues (varchar)

>
> > >> the lvalues column contains comma separted values. e.g. 12, 5, 32
> > >> each individual values matches a field in another table.

>
> > >> How do I write a SQL query that parses the comma separated data? For
> > >> example:
> > >> WHERE user id=30 so that the results look like:

>
> > >> 30 | 12
> > >> 30 | 5
> > >> 30 |32

>
> > >> TIA

>
> > > You don't you build a table structure where each value has its own
> > > record in another table.

>
> > > From:
> > >http://dev.mysql.com/tech-resources/...alization.html
> > > "The first normal form (or 1NF) requires that the values in each
> > > column of a table are atomic. By atomic we mean that there are no sets
> > > of values within a column."

>
> you can use a function called substring_index to separate the values.
> --
> POST BY: lark with PHP News Reader


Please check this link - I think it will do what you want:

http://forge.mysql.com/snippets/view.php?id=4

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 07-01-2007, 11:02 PM   #6
Jerry Stuckle
 
Jerry Stuckle's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default How do I parse a comma separated field?

John Rappold wrote:
> "Captain Paralytic" <paul_lautman@yahoo.com> wrote in message
> news:1181830481.681955.25380@e9g2000prf.googlegrou ps.com...
>> On 14 Jun, 15:03, "John Rappold" <jrapp...@jscoca-k12.org> wrote:
>>> I have a table with the following fields:
>>> tableid (int)
>>> userid (varchar)
>>> lvalues (varchar)
>>>
>>> the lvalues column contains comma separted values. e.g. 12, 5, 32
>>> each individual values matches a field in another table.
>>>
>>> How do I write a SQL query that parses the comma separated data? For
>>> example:
>>> WHERE user id=30 so that the results look like:
>>>
>>> 30 | 12
>>> 30 | 5
>>> 30 |32
>>>
>>> TIA

>> You don't you build a table structure where each value has its own
>> record in another table.
>>
>> From:
>> http://dev.mysql.com/tech-resources/...alization.html
>> "The first normal form (or 1NF) requires that the values in each
>> column of a table are atomic. By atomic we mean that there are no sets
>> of values within a column."
>>

>
> This is a lookup table and the data is already there in the comma separated
> format. I can't change it.
>
> I'm thinking I need to do some sort of SELECT IN and then create a temporary
> table.
>
> Can anyone help me?
>


(Top posting fixed)

Paul is correct. While you might be able to get by with it this time,
you will be much better off in the long run to normalize your database.
Otherwise you will continue to run into similar problems.

In this case, since you have a many-to-many relationship, it's as simple
as building another table. This new table would have two columns - the
id of the original row in your first table, and one of the values from
the multiple-valued column, i.e. (Using names to be clearer - normally I
would be using numeric values for the id's):

Old table
Tom Venice, Rome, Paris, Berlin
**** London, Copenhagen, Paris, Madrid
Harry London, Venice, Rome, Madrid

New table:
Tom Venice
Tom Rome
Tom Paris
Tom Berlin
**** London
**** Copenhagen
**** Paris
**** Madrid
Harry London
Harry Venice
Harry Rome
Harry Madrid

Now it is easy to select who's been to Rome (Tom and Harry), or where
**** has been (London, Copenhagen, Paris and Madrid).

It may be difficult to change this now - but it's going to be even
harder later. And as long as you keep this design you will run into
more problems like this.

P.S. Please don't top post. Thanks.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 07-02-2007, 10:58 AM   #7
Brian M Napoletano
 
Brian M Napoletano's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default How do I parse a comma separated field?

anokun7 wrote:
On Jun 14, 1:41 pm, lark <ham...@sbcglobal.net> wrote:
== Quote from John Rappold (jrapp...@jscoca-k12.org)'s article
This is a lookup table and the data is already there in the comma separated
format. I can't change it.
I'm thinking I need to do some sort of SELECT IN and then create a temporary
able.
Can anyone help me?

"Captain Paralytic" <paul_laut...@yahoo.com> wrote in message
news:1181830481.681955.25380@e9g2000prf.googlegrou ps.com...
On 14 Jun, 15:03, "John Rappold" <jrapp...@jscoca-k12.org> wrote:
I have a table with the following fields:
tableid (int)
userid (varchar)
lvalues (varchar)
the lvalues column contains comma separted values. e.g. 12, 5, 32
each individual values matches a field in another table.
How do I write a SQL query that parses the comma separated data? For
example:
WHERE user id=30 so that the results look like:
30 | 12
30 | 5
30 |32
TIA

> You don't you build a table structure where each value has its own
> record in another table.
> From:
> http://dev.mysql.com/tech-resources/...alization.html
> "The first normal form (or 1NF) requires that the values in each
> column of a table are atomic. By atomic we mean that there are no sets
> of values within a column."
> you can use a function called substring_index to separate the values.


> POST BY: lark with PHP News Reader
>
> Please check this link - I think it will do what you want:
>
> http://forge.mysql.com/snippets/view.php?id=4
>


This is how I personally handle that issue:
You can use the LIKE operator to select the comma-delimited values.
Assuming the values are all known, turning them into a new table
shouldn't be too difficult.

CREATE TABLE lv12
SELECT columns FROM table1
WHERE uid='30' AND lvalues LIKE '%12%';

CREATE TABLE lv5
SELECT columns FROM table
WHERE uid='30' AND lvalues LIKE '%5%';

CREATE TABLE lv32
SELECT columns FROM table
WHERE uid='30' AND lvalues LIKE '%32%';

CREATE TABLE newlayout
SELECT lv12.uid AS uid,
lv12.lvalues AS l12,
lv5.lvalues AS l5,
lv32.lvalues AS l32

FROM lv12
LEFT JOIN lv5
ON lv12.uid = lv5.uid
LEFT JOIN lv32
ON lv12.uid = lv32.uid;

There might be a more elegant way to actually seperate the columns, but
the method here will work.

--
Temi Solo Dio

Brian M Napoletano
napzilla@napoletano.net
http://www.napoletano.net
 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Featured Websites
Free Space
Free Space
Free Space Free Space
Closed Thread
Tags: , , ,




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Echo getting Parse error Jeffrey Ellis PHP 8 07-01-2007 5:09 PM
Why does this produce a parse error? Phil Powell PHP 4 07-01-2007 5:09 PM
Parse error: parse error, expecting `','' or `';'' Warstar PHP 0 07-01-2007 4:14 PM
parse error mr bungle PHP 2 07-01-2007 4:13 PM
What does Parse error: parse error, unexpected T_STRING mean? EV3 PHP 3 07-01-2007 3:56 PM


Featured Websites




All times are GMT +1. The time now is 12:11 AM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.0.0
Cheap Computers
MK PitStop Copyright 2005 - 2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98