We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,278 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Mysql Select Query..

if i have roughly 25 identical tables is it possible to do the following

SELECT tm_name,
`tablename` as Activity
from t1, t2 ,t3 
where date = curdate

or would it be better to add another column to the table thats hidden from the users and automatically populate with the tablename.

4
Contributors
3
Replies
36 Minutes
Discussion Span
10 Months Ago
Last Updated
4
Views
JukesK
Newbie Poster
20 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

The problem is that you will most likely not get the results you want. I suggest you create a query (or view) like this:

SELECT `tm_name`, `date`, 't1' AS TableName FROM t1
UNION
SELECT `tm_name`, `date`, 't2' AS TableName FROM t2
-- and so on
pritaeas
Posting Prodigy
Moderator
9,310 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,465
Skill Endorsements: 86

When you list multiple table names after the FROM keyword, you are essentially creating JOINing tables. Based on your description, it sounds like you are trying to query multiple tables with the same information to produce one result set. For this type of scenario, I would suggest that you use the UNION keyword seperated by your different SELECT statements. For example...

SELECT tm_name FROM table1
UNION
SELECT tm_name FROM table2
UNION
SELECT tm_name FROM table3
JorgeM
Industrious Poster
4,023 posts since Dec 2011
Reputation Points: 297
Solved Threads: 548
Skill Endorsements: 115

As pritaeas said, I'd recommend adding a table idetifier column, since that is the only way of identifying which table is every row from.

Nichito
Posting Virtuoso
1,722 posts since Mar 2007
Reputation Points: 424
Solved Threads: 73
Skill Endorsements: 3

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0697 seconds using 2.7MB