Find Unique Values After using UNION in select query

It was a scenario when I had to fetch data from 3 databases.

Fetching data was not tough as I had used UNION in the select query but the tough part for me was finding the distinct/unique name from all the 3 databases.

I tried different ways to fetch the distinct name from the database but somehow nothing worked as I was using UNION of  3 databases.

I came up with a simple solution of writing the UNION query in a normal way and then storing the data into an array.

After that, I have used array_unique to find the unique name from all the databases.

$sql= "select id, name from db1.users 
UNION 
select id, name from db2.users
UNION
select id, name from db3.users
GROUP BY name
";
//replace db1,db2,db3 with your database names
$res= mysql_query($sql);
$array = array();

while ($data = mysql_fetch_array($res))
{
	$name=$data['name']; //change the name of the field as per requirement
	$array[] = $name;
}	
$names = array_values( array_unique( $array ) );
$names = sort($names); //sort the field as per requirement 
//print_r($names);
foreach( $names as $information_source_name ) {
        echo $information_source_name;
        echo "<br>";
// you can also print the data in tabular format using following lines. Dont forget to add <table> tag before and
//after while loop
//echo'<tr style="background-color:#FFFFFF">';
//echo'<td>'.$information_source_name.'</td>';
// echo "</tr>";
} 
?>

Code written here is very very easy to understand. But still, if you need any explanation, put your doubts in the comment section given below 🙂

233 thoughts on “Find Unique Values After using UNION in select query

  1. Pingback: Google
  2. Pingback: 사다리사이트
  3. Pingback: 먹튀검증
  4. Pingback: 안전놀이터
  5. Pingback: bosch wvh28440
  6. Pingback: female sex toys
  7. Pingback: dual penetration
  8. Pingback: ссылка
  9. Pingback: beast tv
  10. Pingback: Strap Ons
  11. Pingback: top shop london
  12. Pingback: double sided dong
  13. Pingback: چت سکسی
  14. Pingback: 토토검증
  15. Pingback: 안전공원
  16. Pingback: Google
  17. Pingback: 토토사이트
  18. Pingback: cursos online
  19. Pingback: teen sex
  20. Pingback: seahawks
  21. Pingback: male bondage gear
  22. Pingback: Food coupons
  23. Pingback: kegel toys
  24. Pingback: latest vibrator
  25. Pingback: FIFA Coins
  26. Pingback: body wand massager
  27. Pingback: Detroit SEO
  28. Pingback: detox tea
  29. Pingback: multqa.ae
  30. Pingback: cursos gratuitos
  31. Pingback: ultimate ring
  32. Pingback: XXX
  33. Pingback: chwilówki
  34. Pingback: vibrato lesson
  35. Pingback: silver bullet egg
  36. Pingback: evolved sex toy
  37. Pingback: Porn City
  38. Pingback: glow dildo
  39. Pingback: Nerf Gun
  40. Pingback: mail order brides
  41. Pingback: detroit seo
  42. Pingback: brazzers porn
  43. Pingback: top anal sex toys
  44. Pingback: Lawn
  45. Pingback: best penis pump
  46. Pingback: anal sex toys
  47. Pingback: fun88
  48. Pingback: 188bet
  49. Pingback: blue dolphin vibe
  50. Pingback: max results pump
  51. Pingback: webcam sex
  52. Pingback: milf dating
  53. Pingback: cameras ao vivo
  54. Pingback: rencontres sexe
  55. Pingback: butt plug set
  56. Pingback: anal cleaning bag
  57. Pingback: pocket stroker
  58. Pingback: Opblaasband
  59. Pingback: Acronis Cracks
  60. Pingback: cursos gratuitos
  61. Pingback: dani fleshlight
  62. Pingback: gay cock
  63. Pingback: bullet vibe
  64. Pingback: webcambabes
  65. Pingback: webcambabes
  66. Pingback: anal play kit
  67. Pingback: double ended dildo
  68. Pingback: thruster vibe
  69. Pingback: wireless anal plug
  70. Pingback: penis stroker
  71. Pingback: sex toys
  72. Pingback: gay anal toys
  73. Pingback: top rated strapons
  74. Pingback: penis enhancement
  75. Pingback: dildo
  76. Pingback: silicone vibrator
  77. Pingback: Pornland
  78. Pingback: vibrating dildo
  79. Pingback: Photoshop Discount
  80. Pingback: fun surveys
  81. Pingback: i'm feeling good
  82. Pingback: hp 2311x driver
  83. Pingback: ярем
  84. Pingback: dildo reviews
  85. Pingback: big didlo
  86. Pingback: viagra
  87. Pingback: satta king
  88. Pingback: bullet egg
  89. Pingback: best double dildo
  90. Pingback: realistic dildo
  91. Pingback: g spot vibrator
  92. Pingback: silicone dilators
  93. Pingback: gay male toys
  94. Pingback: vibrating stroker
  95. Pingback: bondage toys
  96. Pingback: porn city
  97. Pingback: pornland
  98. Pingback: butterfly vibe
  99. Pingback: Cialis
  100. Pingback: gay men underwear
  101. Pingback: realistic dildo
  102. Pingback: best booksever
  103. Pingback: dual ended stroker
  104. Pingback: dildo reviews
  105. Pingback: bullet massager
  106. Pingback: best paddle
  107. Pingback: bondage
  108. Pingback: Barbie speelgoed
  109. Pingback: stocking lingerie
  110. Pingback: taske til hund
  111. Pingback: Roulette online
  112. Pingback: Julia Ann
  113. Pingback: bdjobs apps
  114. Pingback: Fashion Station
  115. Pingback: Fashion Station
  116. Pingback: Pool Covers
  117. Pingback: geile sex filmpjes
  118. Pingback: gratis films
  119. Pingback: pornofilmpjes
  120. Pingback: liteblue.usps.gov

Leave a Reply