473,396 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

A search withina comma seperated list (Is this possible)

I have a mysql database with a list of companies who supply specific
products

tblSuppliers (simplified)

sID | sName | goodsRefs
1 | comp name | 1,2,3,4,5
2 | company 2 | 2,4
tblGoods (simplified)

gID | gName
1 | fish
2 | bread
3 | apples
4 | bananas
5 | chocolate
The idea is that the comma seperated list relates to the goods they supply

is it possible to show all suppliers who provide bread?

I know of the IN syntax but this only works using SELECT * from tblGoods
WHERE gID in (1,2)

How do i create a SELECT Statement to show the companies containing the ID
2 in their respective comma separted list

Any help would be gratefully appreciated

Craig
Jul 23 '05 #1
11 2511
Craig Keightley wrote:
I have a mysql database with a list of companies who supply specific
products

tblSuppliers (simplified)

sID | sName | goodsRefs
1 | comp name | 1,2,3,4,5
2 | company 2 | 2,4
tblGoods (simplified)

gID | gName
1 | fish
2 | bread
3 | apples
4 | bananas
5 | chocolate
The idea is that the comma seperated list relates to the goods they supply

is it possible to show all suppliers who provide bread?
SELECT * FROM yourtable WHERE goodsRefs LIKE '%2%';

If you have 20 or more goods, then the query would generate false results, but
then you can build a long and complicated query with a handfull OR.

I know of the IN syntax but this only works using SELECT * from tblGoods
WHERE gID in (1,2)
IN() does take a handfull arguments and compares them to the column, the
values has to be of same type, as your goodsRefs is varchar, you can't compare
it to int.

How do i create a SELECT Statement to show the companies containing the ID
2 in their respective comma separted list


You usually build 3 tables

tblSuppliers (simplified)
sID | sName
1 | comp name
2 | company 2

tblGoods (simplified)
gID | gName
1 | fish
2 | bread
3 | apples
4 | bananas
5 | chocolate

tblRefs
sID | gID
1 | 1
1 | 2
1 | 3
1 | 4
1 | 5
2 | 2
2 | 4

You then join the tables when you want something out of them
http://dev.mysql.com/doc/mysql/en/join.html

SELECT tblSuppliers.sName FROM tblSuppliers WHERE tblSuppliers.sID =
tblRefs.sID AND tblRefs.sID='2';

or if you search for more than one, say chocolate and bananas

SELECT tblSuppliers.sName FROM tblSuppliers WHERE tblSuppliers.sID =
tblRefs.sID AND tblRefs.sID IN(4,5);

//Aho
Jul 23 '05 #2
SELECT * FROM yourtable WHERE goodsRefs LIKE '%2%';
I've tried that beofe and the list could have in excess of 500
You usually build 3 tables
isn't that long winded?
The suppliers will be added via a web interface, where they can add a
supplier and choose what products they provide, the 3rd able may not work

"J.O. Aho" <us**@example.net> wrote in message
news:39*************@individual.net... Craig Keightley wrote:
I have a mysql database with a list of companies who supply specific
products

tblSuppliers (simplified)

sID | sName | goodsRefs
1 | comp name | 1,2,3,4,5
2 | company 2 | 2,4
tblGoods (simplified)

gID | gName
1 | fish
2 | bread
3 | apples
4 | bananas
5 | chocolate
The idea is that the comma seperated list relates to the goods they
supply

is it possible to show all suppliers who provide bread?


SELECT * FROM yourtable WHERE goodsRefs LIKE '%2%';

If you have 20 or more goods, then the query would generate false results,
but then you can build a long and complicated query with a handfull OR.

I know of the IN syntax but this only works using SELECT * from tblGoods
WHERE gID in (1,2)


IN() does take a handfull arguments and compares them to the column, the
values has to be of same type, as your goodsRefs is varchar, you can't
compare it to int.

How do i create a SELECT Statement to show the companies containing the
ID 2 in their respective comma separted list


You usually build 3 tables

tblSuppliers (simplified)
sID | sName
1 | comp name
2 | company 2

tblGoods (simplified)
gID | gName
1 | fish
2 | bread
3 | apples
4 | bananas
5 | chocolate

tblRefs
sID | gID
1 | 1
1 | 2
1 | 3
1 | 4
1 | 5
2 | 2
2 | 4

You then join the tables when you want something out of them
http://dev.mysql.com/doc/mysql/en/join.html

SELECT tblSuppliers.sName FROM tblSuppliers WHERE tblSuppliers.sID =
tblRefs.sID AND tblRefs.sID='2';

or if you search for more than one, say chocolate and bananas

SELECT tblSuppliers.sName FROM tblSuppliers WHERE tblSuppliers.sID =
tblRefs.sID AND tblRefs.sID IN(4,5);

//Aho

Jul 23 '05 #3
Craig Keightley wrote:
I have a mysql database with a list of companies who supply specific
products

tblSuppliers (simplified)

sID | sName | goodsRefs
1 | comp name | 1,2,3,4,5
2 | company 2 | 2,4


Having multiple pieces of data in the same field in a table is a BAD
idea, for reasons that you are discovering. Have each entry on a
separate line, or even better (to avoid duplication of sName), arrange
your db as so:

tblSuppliers: sID, sName

tblSupplierGoods: sID, gID

tblGoods: gID, gName

i.e. each company has ONE entry in tblSuppliers linking their name to an
ID, and then an entry in tblSupplierGoods for each individual goods ID
that they supply.

To find which manufacturers supply bread, your query would be:

SELECT tblSuppliers.* FROM
tblSuppliers, tblSupplierGoods, tblGoods
WHERE
tblSupplierGoods.sID = tblSuppliers.sID
AND tblSupplierGoods.gID = tblGoods.gID
AND tblGoods.gName = 'bread'
--
Oli
Jul 23 '05 #4
Craig Keightley wrote:
SELECT * FROM yourtable WHERE goodsRefs LIKE '%2%';


I've tried that beofe and the list could have in excess of 500

SELECT * FROM yourtable WHERE (goodsRefs LIKE '%,2,%') OR (goodsRefs LIKE
'2,%') OR (goodsRefs LIKE '%,2') OR (goodsRefs LIKE '2');

That should cover all the possibilities for the 2, to be in the middle, be
first, be last or be the only one.

You usually build 3 tables


isn't that long winded?
The suppliers will be added via a web interface, where they can add a
supplier and choose what products they provide, the 3rd able may not work


You normaly come to this at the second normal form if I remeber it right
(otherwise it's on the third). It will be a lot easier for you to code your
system and a lot easier to search too and this is what is the point in using a
relation database, otherwise you could just have everything in one textfile
with all data about one company in one long row.
//Aho
Jul 23 '05 #5
I'll give it a try

thanks
"J.O. Aho" <us**@example.net> wrote in message
news:39*************@individual.net...
Craig Keightley wrote:
SELECT * FROM yourtable WHERE goodsRefs LIKE '%2%';


I've tried that beofe and the list could have in excess of 500

SELECT * FROM yourtable WHERE (goodsRefs LIKE '%,2,%') OR (goodsRefs LIKE
'2,%') OR (goodsRefs LIKE '%,2') OR (goodsRefs LIKE '2');

That should cover all the possibilities for the 2, to be in the middle, be
first, be last or be the only one.

You usually build 3 tables


isn't that long winded?
The suppliers will be added via a web interface, where they can add a
supplier and choose what products they provide, the 3rd able may not work


You normaly come to this at the second normal form if I remeber it right
(otherwise it's on the third). It will be a lot easier for you to code
your system and a lot easier to search too and this is what is the point
in using a relation database, otherwise you could just have everything in
one textfile with all data about one company in one long row.
//Aho

Jul 23 '05 #6
J.O. Aho wrote:
SELECT * FROM yourtable WHERE (goodsRefs LIKE '%,2,%') OR (goodsRefs
LIKE '2,%') OR (goodsRefs LIKE '%,2') OR (goodsRefs LIKE '2');

That should cover all the possibilities for the 2, to be in the middle,
be first, be last or be the only one.


You should be able to do this with regular expression matching too.
MySQL regular expressions include a "word boundary" marker:

SELECT * FROM tblSuppliers
WHERE goodsRefs REGEXP '[[:<:]]2[[:>:]]';

You also could do a join so you could get the gName from the tblGoods
table too.

SELECT s.*, g.gName
FROM tblSuppliers AS s INNER JOIN tblGoods AS g
ON s.goodsRef REGEXP CONCAT('[[:<:]]', g.gID, '[[:>:]]');

Regards,
Bill K.
Jul 23 '05 #7
"Craig Keightley" <do**@spam.me> wrote in message
news:42**********************@news-text.dial.pipex.com...
I have a mysql database with a list of companies who supply specific
products

tblSuppliers (simplified)

sID | sName | goodsRefs
1 | comp name | 1,2,3,4,5
2 | company 2 | 2,4
tblGoods (simplified)

gID | gName
1 | fish
2 | bread
3 | apples
4 | bananas
5 | chocolate
The idea is that the comma seperated list relates to the goods they supply

is it possible to show all suppliers who provide bread?

I know of the IN syntax but this only works using SELECT * from tblGoods
WHERE gID in (1,2)

How do i create a SELECT Statement to show the companies containing the ID 2 in their respective comma separted list

Any help would be gratefully appreciated

Craig


Use the mysql function FIND_IN_SET. I believe the IN function works
similarly or may even be an alias.

-Joe
Jul 23 '05 #8
"Craig Keightley" <do**@spam.me> wrote in message

I have a mysql database with a list of companies who supply specific
products

tblSuppliers (simplified)

sID | sName | goodsRefs
1 | comp name | 1,2,3,4,5
2 | company 2 | 2,4
tblGoods (simplified)

gID | gName
1 | fish
2 | bread
3 | apples
4 | bananas
5 | chocolate
The idea is that the comma seperated list relates to the goods they supply

is it possible to show all suppliers who provide bread?

I know of the IN syntax but this only works using SELECT * from tblGoods
WHERE gID in (1,2)

How do i create a SELECT Statement to show the companies containing the
2 in their respective comma separted list

Any help would be gratefully appreciated

Craig

Craig,
What you want to do is probably not a good idea. You have a
denormalized data structure with an arbitrary length embedded
array.

You can simplify your code, simplify future maintenance and
increase your performance by breaking the array out into an
attribute:value table indexed on both attributes and values.
Your table would take the form sID:gID and would look like:

ABC fish
ABC bread
ABC apples
XYZ fish

(Instead of using the names of the companies and goods use the
index values for the tables that contain them.)

Using this approach you can get the information you require
using simple joins and let the RDBMS do the work for you.

HTH

Jerry
Jul 23 '05 #9
The SQL :

SELECT * FROM yourtable WHERE (goodsRefs LIKE '%,2,%') OR (goodsRefs
LIKE '2,%') OR (goodsRefs LIKE '%,2') OR (goodsRefs LIKE '2');

works just as i needed, thanks for that

Craig

"Bill Karwin" <bi**@karwin.com> wrote in message
news:d1*********@enews2.newsguy.com...
J.O. Aho wrote:
SELECT * FROM yourtable WHERE (goodsRefs LIKE '%,2,%') OR (goodsRefs LIKE
'2,%') OR (goodsRefs LIKE '%,2') OR (goodsRefs LIKE '2');

That should cover all the possibilities for the 2, to be in the middle,
be first, be last or be the only one.


You should be able to do this with regular expression matching too. MySQL
regular expressions include a "word boundary" marker:

SELECT * FROM tblSuppliers
WHERE goodsRefs REGEXP '[[:<:]]2[[:>:]]';

You also could do a join so you could get the gName from the tblGoods
table too.

SELECT s.*, g.gName
FROM tblSuppliers AS s INNER JOIN tblGoods AS g
ON s.goodsRef REGEXP CONCAT('[[:<:]]', g.gID, '[[:>:]]');

Regards,
Bill K.

Jul 23 '05 #10
is it possible to compare acomma separated list aginst another

eg comma list 1 => 1,2,3,4,5
comma list 2 => 3,5,6,9

can you check that 3 is in both, and 5 is in both, therfore they match???
"jerry gitomer" <jg******@verizon.net> wrote in message
news:xcWZd.6994$I16.5073@trndny03...
"Craig Keightley" <do**@spam.me> wrote in message
I have a mysql database with a list of companies who supply specific
products

tblSuppliers (simplified)

sID | sName | goodsRefs
1 | comp name | 1,2,3,4,5
2 | company 2 | 2,4
tblGoods (simplified)

gID | gName
1 | fish
2 | bread
3 | apples
4 | bananas
5 | chocolate
The idea is that the comma seperated list relates to the goods they supply

is it possible to show all suppliers who provide bread?

I know of the IN syntax but this only works using SELECT * from tblGoods
WHERE gID in (1,2)

How do i create a SELECT Statement to show the companies containing the
2 in their respective comma separted list

Any help would be gratefully appreciated

Craig

Craig,
What you want to do is probably not a good idea. You have a denormalized
data structure with an arbitrary length embedded array.

You can simplify your code, simplify future maintenance and increase your
performance by breaking the array out into an attribute:value table
indexed on both attributes and values. Your table would take the form
sID:gID and would look like:

ABC fish
ABC bread
ABC apples
XYZ fish

(Instead of using the names of the companies and goods use the index
values for the tables that contain them.)

Using this approach you can get the information you require using simple
joins and let the RDBMS do the work for you.

HTH

Jerry

Jul 23 '05 #11
Craig Keightley wrote:
is it possible to compare acomma separated list aginst another

eg comma list 1 => 1,2,3,4,5
comma list 2 => 3,5,6,9

can you check that 3 is in both, and 5 is in both, therfore they match???
"jerry gitomer" <jg******@verizon.net> wrote in message
news:xcWZd.6994$I16.5073@trndny03...
"Craig Keightley" <do**@spam.me> wrote in message
I have a mysql database with a list of companies who supply specific
products

tblSuppliers (simplified)

sID | sName | goodsRefs
1 | comp name | 1,2,3,4,5
2 | company 2 | 2,4
tblGoods (simplified)

gID | gName
1 | fish
2 | bread
3 | apples
4 | bananas
5 | chocolate
The idea is that the comma seperated list relates to the goods they supply

is it possible to show all suppliers who provide bread?

I know of the IN syntax but this only works using SELECT * from tblGoods
WHERE gID in (1,2)

How do i create a SELECT Statement to show the companies containing the
2 in their respective comma separted list

Any help would be gratefully appreciated

Craig


Craig,
What you want to do is probably not a good idea. You have a denormalized
data structure with an arbitrary length embedded array.

You can simplify your code, simplify future maintenance and increase your
performance by breaking the array out into an attribute:value table
indexed on both attributes and values. Your table would take the form
sID:gID and would look like:

ABC fish
ABC bread
ABC apples
XYZ fish

(Instead of using the names of the companies and goods use the index
values for the tables that contain them.)

Using this approach you can get the information you require using simple
joins and let the RDBMS do the work for you.

HTH

Jerry



Yes, you can -- but you shouldn't. To find values in your comma
separated list you would do a SELECT ... LIKE ... To work with
the output of two such queries you would use a second SELECT ...
LIKE ... in a sub-query.

HTH

Jerry
Jul 23 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Craig Keightley | last post by:
is it possible to compare acomma separated list aginst another eg comma list 1 => 1,2,3,4,5 comma list 2 => 3,5 can you check that 3 is in both, and 5 is in both, therfore they match??? the...
3
by: Brad Joss | last post by:
Scenario: Table 1 (a id, b name) Table 2 (a FKid, d value) A standard join on a gives me something like: a1 b1 d1 a1 b1 d2 What I want is:
2
by: Pmb | last post by:
I'm trying to learn the syntax for initializing objects in a comma separated list. Below is an example program I wrote to learn how to do this (among other things). While I understand how to...
3
by: Gary Smith | last post by:
Hi, I've got a field that contains a list of rooms. In most cases, this contains a single ID. However, under some circumstances, the field may contain a list of two IDs which are broken by a...
1
by: Hennie7863 | last post by:
Hi i want to create a table as follows : if exists (select * from dbo.sysobjects where id = object_id(N'') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table GO Create table...
3
by: dfetrow410 | last post by:
I need make a comma seperated list, but whwn I build the list I get a comma at the end. How do I remove it? foreach (ListItem lst in REIPropertyType.Items) { if (lst.Selected == true) {...
0
by: Kristi | last post by:
I need to create a CL program that will take a PF, and create a tab delimited file that has comma seperated column headings as the first record. I know i can use cpytostmf/cpytoimpf to create the...
6
by: orajit | last post by:
Hi, I wanted to list the all columns of a emp table . The output should be comma seperated and list of column should come in brakets like (enam,sal,jdate,job). The below code gives me proper...
1
by: udaypawar | last post by:
Hi All, I have one problem here with mysql stored procedures. I have a list of ids seperated by comma e.g., (" 'A', 'B', 'C' "). I am passing the same to mysql stored procedure as a parameter....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.