There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
SQL Join Question


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
dhirsch226's Avatar
Junior Member with 1 posts.
 
Join Date: Sep 2007
Experience: Intermediate
28-Sep-2007, 06:52 PM #1
SQL Join Question
Hi. I am trying to set up a PHP/MySQL backend for a webpage that will display scientific abstracts. The datasets will be small, so performance is not a big issue here - I'm trying to offload the content maintenance onto the content provider, so I don't have to keep cleaning up his ugly MS Word files to post the content.

I've got two tables: author, and abstract

author has columns that include name and id
abstract has columns that include authorList, which is a comma-delimited set of id numbers corresponding to author.id. Here is an example:


Code:
example author data:
+----------+----+
| name     | id |
+----------+----+
| jones    | 1  |
| smith    | 2  |
| allen    | 3  |
+----------+----+

example abstract data:
+-------------+------------+
| content     | authorList |
+-------------+------------+
| blahblah    | 2,3        |
| foobar      | 1          |
| fooblah     | 3,1,2      |
+-------------+------------+


I'm trying to construct a SELECT statement that will translate the authorList into a human-readable list of names, and being fairly new at MySQL this involved, I am stumped and hope you can help. (Well, I can think of brute-force PHP methods to do it, but it seems like this must be a common SQL task and have an easy solution)

I would like to get the following type of output:

Code:
+-------------+-------------------+
| content     | authorTextList    |
+-------------+-------------------+
| blahblah    | smith,allen       |
| foobar      | jones             |
| fooblah     | allen,jones,smith |
+-------------+-------------------+


Note: this is not homework for some class. This is for the following website that I manage: http://mbvo.wwu.edu. I believe the MySQL version is at least 4.1.
Thanks, in advance
Dave Hirsch
MMJ's Avatar
MMJ MMJ is offline
Distinguished Member with 3,208 posts.
 
Join Date: Oct 2006
29-Sep-2007, 03:24 PM #2
I seriously doubt that this is possible with SQL only. Heres some PHP that should work though.

PHP Code:
<?php
echo "<table border='1'>
<tr>
<th>content</th>
<th>authorTextList</th>
</tr>"
;

$query mysql_query("SELECT content, authorList FROM `abstract`");
while (
$column mysql_fetch_array($query))
{
    
$authorList explode(','$column['authorList']);
    echo 
"<tr>";
    echo 
"<td>" $column['content'] . "</td>\n<td>";
    for (
$i 0$i <= (count($authorList)-1); $i++)
    {
        
$idName mysql_fetch_array(mysql_query("SELECT name, id FROM `author` WHERE id = {$authorList[$i]}"));
        echo 
implode(","$idName);
    }
    echo 
"</td></tr>";
}

echo 
"</table>";
?>

Last edited by MMJ : 29-Sep-2007 03:32 PM.
Reply


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

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
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 12:38 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.