473,545 Members | 2,663 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL Query: Return fields corresponding to comma separated ids in other table

I'm attempting a query that gathers product data for a particular
product id.

One of the items is designer(s) which can be more than one.

The product table has comma separated id's of the designers in the
designers table (which has fields like: id, fname, lname).

How can my select return the possibly several designers for a product,
with the rest of the row data, like price, name, etc.?

I'm using 4.01.

Thanks,
s7

Apr 12 '06 #1
3 6107
st******@hotmai l.com wrote:
I'm attempting a query that gathers product data for a particular
product id.

One of the items is designer(s) which can be more than one.

The product table has comma separated id's of the designers in the
designers table (which has fields like: id, fname, lname).
You should really have another table, which references both the designer
table and the product table. You are modelling a many-to-many
relationship, and this requires an additional table.

Using comma-separated lists to avoid the additional many-to-many table
has several disadvantages:
- Difficult to join to the designers table (as you've discovered)
- Difficult to remove an id from the list
- There's a hard limit to the number of id's that can fit in the string
- No way to automatically enforce that the id's actually reference
existing id's in the designers table
- No way to automatically enforce that the format of comma-separated
integers is followed; that is, you can enter "1,2,3,banana,6 " and
nothing happens

All of these difficulties are made much easier if you use an additional
table for the many-to-many relationship, with foreign key references.
How can my select return the possibly several designers for a product,
with the rest of the row data, like price, name, etc.?


This might work:

SELECT p.*, d.*
FROM product AS p INNER JOIN designer AS d
ON p.designer_id_l ist RLIKE CONCAT('[[:<:]]', d.id, '[[:>:]]')

Then again, if the list isn't formed correctly, and the join fails in a
few cases, you'll never know if you've lost one of the designers.

Regards,
Bill K.
Apr 12 '06 #2
thanks for the clues. unfortunately, the designer ids are in one
column, separated by commas... i wonder if i could just match on the
first one, better than none...

the php code the app uses does an explode or implode to read the
designers on the product pages, but i'm not sure of the query they use.

s7

Apr 12 '06 #3

Bill Karwin wrote:
st******@hotmai l.com wrote:
I'm attempting a query that gathers product data for a particular
product id.

One of the items is designer(s) which can be more than one.

The product table has comma separated id's of the designers in the
designers table (which has fields like: id, fname, lname).


You should really have another table, which references both the designer
table and the product table. You are modelling a many-to-many
relationship, and this requires an additional table.

Using comma-separated lists to avoid the additional many-to-many table
has several disadvantages:
- Difficult to join to the designers table (as you've discovered)
- Difficult to remove an id from the list
- There's a hard limit to the number of id's that can fit in the string
- No way to automatically enforce that the id's actually reference
existing id's in the designers table
- No way to automatically enforce that the format of comma-separated
integers is followed; that is, you can enter "1,2,3,banana,6 " and
nothing happens

All of these difficulties are made much easier if you use an additional
table for the many-to-many relationship, with foreign key references.
How can my select return the possibly several designers for a product,
with the rest of the row data, like price, name, etc.?


This might work:

SELECT p.*, d.*
FROM product AS p INNER JOIN designer AS d
ON p.designer_id_l ist RLIKE CONCAT('[[:<:]]', d.id, '[[:>:]]')

Then again, if the list isn't formed correctly, and the join fails in a
few cases, you'll never know if you've lost one of the designers.

Regards,
Bill K.

bill thanks - this works great - though when there are more than one
designer, i seem to be getting the last. thanks again, mostly there is
only one designer, and better the last when there are two than none!

thanks again,
s7

Apr 26 '06 #4

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

Similar topics

13
5534
by: aaron | last post by:
I have a question about (i think) joining. If I have a table in a database that has this info: key - name - favorite 1 - john - 2 2 - judy - 3 3 - joe - 1 the favorite icecream table is this:
4
2373
by: Bacci | last post by:
I have two tables. The first is "Locations" which has 52,000 zip codes w/cooresponding latitudes and longitudes. The second "Suppliers" has 2,000 company names and addresses. The user enters a zip code and the cooresponding latitude and longitude is return. $sql = "SELECT l.latitude, l.longitude "; $sql .= "FROM Locations l"; $sql .=...
6
1453
by: StressedMonkey | last post by:
Hi, I'm not too hot on my manipulation of strings. I have a database which cintains names in the format (Upper case also) LASTNAME, FIRSTNAME I want to search against that database, but the data on the form to produce the string will be input in the following format: Firstname Lastname
12
29053
by: insomniux | last post by:
Hi, I'n in an environment where I cannot make stored procedures. Now I need to make a query with a subquery in the SELECT part which gives a comma separated list of results: SELECT p.id, listFunction(SELECT name FROM names WHERE name_parent=p.id) AS 'nameList' FROM projects AS p
3
3178
by: Roland Burr | last post by:
I have a database where each row has a set of keywords separated by commas eg . ROW 1 ID,<other columns>,"green,hat,man,boat" ROW 2 ID,<other columns>,"green,boat,seaside" ROW 3 ID,<other columns>,"red,hat" I want users to be able to find similar records to the one they are looking it (this is art BTW). ie if they are looking at row 1...
6
4829
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID SalesManName AT Alan Time
4
1587
by: johnny | last post by:
hi all, I hope it is easier for you to answer than for me trying to explain it... In a database I have some tables , each one has some mandatory fields at the beginning and a couple at the end. In the middle each table can have some additional fields from 0 to n depending on how many fields have been inserted by who created the table.
0
2434
by: Chuck36963 | last post by:
Hi all, I've been working on a listing problem and I can't figure out how to work it out. I have looked far and wide on the web to find answers, but I'd like other peoples input on my project in the whole. I really need MySQL wizz to give me a hand (and maybe refer me to books to get me to the wizz level myself). First off, english is a...
0
4190
MMcCarthy
by: MMcCarthy | last post by:
This is a module that exports information from a Query or a Table to comma separated values in a text format, or using other symbol! It is very helpful for sharing information between the applications! The function that does this work is: create_file_from_SQL_SEP(SQL, File_name, sep) As Integer Parameters: SQL: You can pass a SQL...
0
7490
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7682
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7449
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6009
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5069
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3479
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3465
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.