Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need help adding 2 table column values into code

Author  Topic 

iNko
Starting Member

19 Posts

Posted - 2012-12-16 : 08:46:36
Hey, i got this code that selects values from 1 table (table Messages), depending on values from other 2 tables (tables TableA and TableB):

SELECT
a.message_name , a.message_date
FROM Messages a
INNER JOIN
(
SELECT message_id
FROM TableA
WHERE username = 'name1'
UNION ALL
SELECT message_id
FROM TableB
WHERE usersurname = 'surname1'
)b ON a.message_id= b.message_id

I want to modify it by adding 1 more column from TableA (column name - Dates) and TableB (column name - Dates) and merging their values into one..

Basically my result with the code i have is this:
message_name || message_date
message1 || 2012
message2 || 2013

I want it to be like this:
message_name || message_date || Dates
message1 || 2012 || 06
message2 || 2013 || 05

Heres the stucture of the tables to make it more clear what im asking:

table - Messages:
message_id || message_name || message_date
1 || message1 || 2012
2 || message2 || 2013

table - TableA:
message_id || Dates
1 || 06

table - TableB:
message_id || Dates
2 || 05

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2012-12-16 : 09:20:07
[code]SELECT
a.message_name , a.message_date,b.Dates
FROM Messages a
INNER JOIN
(
SELECT message_id,Dates
FROM TableA
WHERE username = 'name1'
UNION ALL
SELECT message_id,Dates
FROM TableB
WHERE usersurname = 'surname1'
)b ON a.message_id= b.message_id[/code]
Go to Top of Page

iNko
Starting Member

19 Posts

Posted - 2012-12-16 : 09:31:30
thank you very much sir
Go to Top of Page
   

- Advertisement -