Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with MySQL Query

Status
Not open for further replies.

Boochy

Programmer
May 24, 2011
2
US
I have run into an issue which I haven't been able to figure out yet. I have two MySQL tables with similar data and I want to return all the data from both tables based on a userid and timestamp. How would I construct the query to accomplish this...examples of my two tables are below.

Counts1 table: user_id, start_date_time, end_date_time, count_type

Counts2 table: count_by_ids, start_date_time, end_date_time, count_type

user_id from Counts1 table contains similar users as what is located in Counts2 table count_by_ids. All user_ids or count_by_ids may not be in both tables, so I need to return all of them with them arranged in descending order by either start_date_time.

Any help is greatly appreciated.

Thanks,
Boochy
 
Hi

Code:
[b]select[/b]
user_id[teal],[/teal] start_date_time[teal],[/teal] end_date_time[teal],[/teal] count_type
[b]from[/b] counts1

[b]union all[/b]

[b]select[/b]
count_by_ids[teal],[/teal] start_date_time[teal],[/teal] end_date_time[teal],[/teal] count_type
[b]from[/b]
counts2

[b]order[/b] [b]by[/b] start_date_time [b]desc[/b]
For more see MySQL | SQL Statement Syntax | Data Manipulation Statements | SELECT Syntax | UNION Syntax.


Feherke.
 
Hi Feherke,

Apparently I was given faulty information. This is a good start, however I just found out that the columns don't match up as nicely as I was originally lead to believe.

The actual columns are as follows:

Counts1 = user_id, start_date_utc, end_date_utc

and

Counts2 = count_by_ids, start_date, end_date

Also need to incorporate a > date into the where clause of the query.

Thanks for your help!!!

Boochy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top