Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-18-2012, 08:04 PM   PM User | #1
Tanoshimi
New to the CF scene

 
Join Date: Jan 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Tanoshimi is an unknown quantity at this point
Question How do I link to a table from an XML list of id's?

I know it's generally poor DB design to have more than one piece of data in a single field, but I have the following setup:

Database called Library
Table called Authors
Table called Books

Books links to Author via it's primary key. Since a book can have more than one author, I have a column in Books called Authors, that contains XML list of all the authors id's.

Code:
<author_id>782395</author_id>
<author_id>234523</author_id>
I can link the two table with an ExtractValue command, and it works to get one of the authors (whichever one I select via the XPath statement), but I need to get all of them.

Is there a way to do this? I want to be able to search for all books by a particular Author, even if they're 5th or 6th id in the list.
Tanoshimi is offline   Reply With Quote
Old 01-19-2012, 12:00 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Ummm...why would you store XML data in a DB field in the first place?

But ignoring that...

Yes, it is generally poor DB design to have any kind of delimited list in a single field.

And yes, it leads you to doing things such as using XPath that you should never do.

Isn't there any way you can fix this DB design? Surely it wouldn't be that hard to run a one-time process that would go through an normalize the database and create a many-to-many table of BookAuthors. You would do that one time and then you'd have the performance--and query capabilities--that you are looking for.

Code:
CREATE TABLE BookAuthors (
     bookid INT REFERENCES books(bookid),
     authorid INT REFERENCES authors(authorid),
     PRIMARY KEY (bookid, authorid)
);
***********

Having said all the above, you *COULD* do this with a really ugly query.

Code:
SELECT b.bookname, a.authorname
FROM books AS b, authors AS a
WHERE b.Authors LIKE CONCAT( '%>', a.authorid, '<%' )
Using LIKE will give you pretty ugly performance, so I sure hope your database is relatively tiny.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Tags
link, mysql, xml

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:59 AM.


Advertisement
Log in to turn off these ads.