Connecting Tech Pros Worldwide Forums | Help | Site Map

determine table in union statement

Member
 
Join Date: Jun 2008
Posts: 97
#1: Jul 22 '09
hi to all

i am using union in select statement like this

Expand|Select|Wrap|Line Numbers
  1. mysql> SELECT * FROM t1 UNION SELECT * FROM t2 UNION SELECT * FROM t3;
  2.  
please tell me that is it possible that i can check value is come from which table (t1,t2 or t3)

let me know if i am not clear in my question

Member
 
Join Date: Jun 2008
Posts: 97
#2: Jul 22 '09

re: determine table in union statement


i have done it by doing this

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.     articles.id AS id,
  3.     articles.title_article AS title,
  4.     articles.contents AS contents,
  5.     1 as tableid 
  6. FROM articles 
  7. WHERE 
  8.     (articles.status = '1' 
  9.         and (articles.title_article like '%political news%' or articles.contents LIKE '%political news%')
  10.     ) 
  11. UNION ALL 
  12. SELECT 
  13.     press_release.id,
  14.     press_release.title,
  15.     press_release.description,
  16.     2 as tableid 
  17. FROM press_release 
  18. WHERE 
  19.     (press_release.status = '1' 
  20.         and (press_release.title like '%political news%' or press_release.author LIKE '%political news%' or press_release.description like '%political news%')
  21.     ) 
  22. UNION 
  23. SELECT 
  24.     latest_news.id,
  25.     latest_news.title,
  26.     latest_news.news,
  27.     3 as tableid 
  28.  FROM latest_news 
  29.  WHERE 
  30.     (latest_news.status = '1' 
  31.         and (latest_news.title like '%political news%' or latest_news.news LIKE '%political news%')
  32.     ) 
  33. LIMIT 0, 50
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,759
#3: Jul 22 '09

re: determine table in union statement


Hi.

I'm glad you found a solution!
Thank you for sharing it with us.
Reply