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 🙂

407 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
  121. Pingback: zutkm
  122. Pingback: flights
  123. Pingback: penis pump review
  124. Pingback: luksus hundeseng
  125. Pingback: best nipple clamps
  126. Pingback: beeg
  127. Pingback: مساج مصر
  128. Pingback: one.walmart.com
  129. Pingback: type 1 diabetes
  130. Pingback: compte outlook
  131. Pingback: top rated vibrator
  132. Pingback: finger vibrator
  133. Pingback: podcast
  134. Pingback: situs poker
  135. Pingback: g spot stimulation
  136. Pingback: my first vibrator
  137. Pingback: blowjob
  138. Pingback: Free Funding
  139. Pingback: penis rings
  140. Pingback: daftar maxbet
  141. Pingback: cs go hacks
  142. Pingback: Hotmail login
  143. Pingback: Gmail Login
  144. Pingback: Hooligan movie
  145. Pingback: mide 675
  146. Pingback: xxx
  147. Pingback: YtMp3
  148. Pingback: male prostate toys
  149. Pingback: onewalmart.com
  150. Pingback: domain
  151. Pingback: wizkid
  152. Pingback: PPPD-780
  153. Pingback: vibrator review
  154. Pingback: anal sex toys
  155. Pingback: anal training plug
  156. Pingback: waterproof rabbit
  157. Pingback: step mom sleeping
  158. Pingback: cheap follower
  159. Pingback: seo merimbula
  160. Pingback: gay dildo videos
  161. Pingback: prepaidgiftbalance
  162. Pingback: nhdtb 305
  163. Pingback: 192.168.254.254
  164. Pingback: cawd 003
  165. Pingback: dildo with balls
  166. Pingback: buttplug
  167. Pingback: boy jockstraps
  168. Pingback: app for laptop
  169. Pingback: exotic cartridges
  170. Pingback: Boston Car Service
  171. Pingback: Minyak Kelapa
  172. Pingback: dank vapes
  173. Pingback: Family Lawyer
  174. Pingback: Vape Pens For Sale
  175. Pingback: Weed for sale
  176. Pingback: weight loss
  177. Pingback: online advertising
  178. Pingback: Modafinil Online
  179. Pingback: ABP 898
  180. Pingback: walmart wire login
  181. Pingback: my gift card site
  182. Pingback: T shirts
  183. Pingback: MEYD-525
  184. Pingback: Women clothing
  185. Pingback: 618media.com
  186. Pingback: Zion
  187. Pingback: Ioe business
  188. Pingback: Ccleaner Crack
  189. Pingback: diazepam for sale
  190. Pingback: marketing Canada
  191. Pingback: bondage gear
  192. Pingback: nipple toys
  193. Pingback: Judi Slot Online
  194. Pingback: IPX 363
  195. Pingback: walmartone.com
  196. Pingback: prostate vibrator
  197. Pingback: SEO Vancouver
  198. Pingback: pizza near me
  199. Pingback: PPPD-789
  200. Pingback: daftar joker123
  201. Pingback: realistic dildo
  202. Pingback: Escorts in Mumbai
  203. Pingback: HUNTA 653

Leave a Reply