Jump to content

[SOLVED] Select All Values From Two Tables


The Little Guy

Recommended Posts

Ask yourself which is the common column in both tables that will tie the two tables together?

 

select

        *

from  email_emails e

join    email_outbox o

on    e.to = o.from      # specify the column link between the two tables

 

where e.to = 'admin@dudeel.com'

 

Link to comment
Share on other sites

The Little Guy, when posting please put more details than just "doesn't work". You're not really giving us enough info on what you do want or expect as output/result. Show us example of data, and example of the result you want/expect.

 

The query in your original post has two tables, but the "where" clause has no join criteria to connect/tie the two tables together. Only you know what the columns are in both table that can match rows together.

 

If you want ALL rows where the "to" column in the email_emails table matches the "from" column in the email_outbox table, then you can get rid of the "where" clause in the query in my previous post.

 

Using the "to" to match a "from" in the other table doesn't make much sense to me, but I'm using it because that's all you posted and the only thing I have to go on. Ask yourself, what is the common data from both tables that can connect them together?

 

 

Link to comment
Share on other sites

OK, I have two tables. One for outbox (email_outbox) message and one for inbox (email_emails) messages.

 

The ONLY two columns that match each other are the email_outbox.from and email_emails.to. Those two columns are the ONLY ones that match each other.

 

They both contain an email address in them. On the user interface side, if the user clicks on "All", it should show all emails that the user has send and/or received.

 

My current displays one row about 20 - 30 times.

 

What am expecting is:

10 inbox results and 8 outbox results.

 

I think I should be using a Union... but... I don't have the same number of columns in both tables.

Link to comment
Share on other sites

Something like:

 

(select `to` as email_addr, `subject`, `body`, 'inbox' as type from email_emails where `to` = 'admin@dudeel.com')

union all

(select `from` as email_addr, `subject`, `body`, 'outbox' as type from email_outbox where `from` = 'admin@dudeel.com')

Link to comment
Share on other sites

But I need all the information from both tables...

 

Is there a way to make temporary columns on the fly so there is an even number of columns?

Yes, what fenway said, but that's why I gave you "type" to show an example of how to add a new value (wherever you want it at).

 

 

Link to comment
Share on other sites

Yes, what fenway said, but that's why I gave you "type" to show an example of how to add a new value (wherever you want it at).

 

Oops didn't see that.

 

Anyways, one more question I think. With the new row(s), can I assign them a temporary value too?

 

I would also like to auto create a field that shows what table the result came from, so I can do something with it in my php according to if it was an inbox message or outbox message.

Link to comment
Share on other sites

Anyways, one more question I think. With the new row(s), can I assign them a temporary value too?

 

I would also like to auto create a field that shows what table the result came from, so I can do something with it in my php according to if it was an inbox message or outbox message.

For sure!

 

SELECT sharedCol1, null as col2, sharedCol3, 'outbox' as source from table1

UNION ALL

SELECT sharedCol1, col2, sharedCol3, 'inbox' as source from table2

 

Link to comment
Share on other sites

I would also like to auto create a field that shows what table the result came from, so I can do something with it in my php according to if it was an inbox message or outbox message.

LOL ... you're kiddin right?  Once again, that's what the "type" column is for in my example!  :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.