Jenn
Jenn

Reputation: 49

Mysql group columns and sort in Alphabetical order - PHP invoved

I have two columns titled word and word2. They each have a word listed inside. My goal is to combine word and word2. If a word is repeating then combine these words as ONE WORD and then place all the words in alphabetical order (see website).

enter image description here

  1. Need for word and word2 to combine into one word only if the word are repeating.
  2. Need for them to be in alphabetical order

This code was given to me and now I need help because I am adding another column but I do not know how to get the mysql and php code to work together to have ONE NAME SHOWING and put them in ALPHABETICAL ORDER. Please visit my website here to see what I am referring to, thank you.

HERE IS MY CODE - Can you please respond with code. If I new the code I would not be here, any help would be great.

$sql = mysqli_query($db_conx, "SELECT word, word2 FROM articles");

while ($row = mysqli_fetch_array($sql)) { 
$link = $row['link'];
$word_array = explode(", ", $row['word']); $unique_word_array = array_unique($word_array);
asort($unique_word_array);

foreach($unique_word_array as $key) 
{ 
        echo "<div class='cloudbox tag'><a href = '../../articlestagresults.php?word=" . $key . "'> " . $key . "</a></div>"; }

}

Upvotes: 0

Views: 49

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269493

You can use union:

select word
from articles
union
select word2
from articles
order by word;

union removes duplicates.

Upvotes: 3

Related Questions