0

hi ,

I want to split the field by comma and that should check the values in another table and display multiple values as given below.

Here is my table format:

Table1:
======

id : nos

1 12,13,14
2 14
3 14,12

Table2:
=======

id : values

12 PHP
13 JAVA
14 C++

Now , I want output like this:
=============================


1 PHP, JAVA, C++
2 C++
3 C++, PHP

How to fetch this using mysql query?

Thanks in advance.

- Gnaniyar Zubair

3
Contributors
2
Replies
4
Views
8 Years
Discussion Span
Last Post by slmsakthi
0

What you want to do is:

$query = "SELECT id,nos FROM Table1";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
    echo $row['id'];
    $values = split(",",$row['nos']);
    $first_entry = true;
    foreach ($values as $one_nos)
    {
        $query2 = "SELECT values FROM Table2 WHERE id = '" . $one_nos . "'";
        $result2 = mysql_query($query2);
        $row2 = mysql_fetch_array($result2);
        if ($first_entry == true)
        {
            echo $row2['values'];
        }
        else
        {
            echo "," . $row2['values'];
        }
    }
    echo "<br />";
}

I'm pretty sure that should return the correct result for you.

-1

hi,
I'm also having the same problem but the difference is i need to display in asp .net
Pls help me on this

Table1:
======

id : nos

1 12,13,14
2 14
3 14,12

Table2:
=======

id : values

12 PHP
13 JAVA
14 C++

Now , I want output like this:
=============================


1 PHP, JAVA, C++
2 C++
3 C++, PHP


thanks in advance

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.