Jump to content

merge and sort two columns in same table


fohanlon

Recommended Posts

Hi Guys

 

I have a members table with 3 fields I want to pull back to populate a dynamic drop down.

 

I have something like this:

 

SELECT field1, field2, field3 from members ORDER BY field2, field3 ASC

 

Heres what I want to do.  I need to list the results sorted ASC with field2 and field3 merged for example, say field2 has

 

BAD

DAD

MAM

 

and field2 has

 

CAT

ELEPHANT

SNAKE

 

I need the result to be listed

 

BAD

CAT

DAD

ELEPHANT#

MAM

SNAKE

 

Do you see what I mean? What is the mysql code for this?

 

thanks,

 

fergal.

 

It would just be like any example you find out there, except with 2 options in the loop:

 

<?php
  $result = mysql_query("SELECT field1, field2, field3 from members ORDER BY field2, field3 ASC");
  print '<select name="mytest">';
  while($row = mysql_fetch_assoc($result)){
    print '<option value="'.htmlspecialchars($row['field2']).'">'.htmlspecialchars($row['field2']).'</option>';
    print '<option value="'.htmlspecialchars($row['field3']).'">'.htmlspecialchars($row['field3']).'</option>';
  }
  print '</select>';
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.