user5381217
user5381217

Reputation:

mysql and php - get 3 or more tables in one select?

I want to show for my users his last activities. For this I have 3 tables that I want to show order by date the news.

eg:

You sell product Y (table sell)
UserX put your product Y in favorite list (table favorite)
You have a product question (table questions)
You sell product Y (table sell again)
...

So I want to get from different tables the user activities (seller) and show this alerts for him. Is it possible? any example how to do this?

table sell:

id
seller
customer
product_name
data

table favorite:

product_id
seller
customer
data

table question:

product_id
seller
customer
question
answer
data

Upvotes: 2

Views: 35

Answers (1)

Hotdin Gurning
Hotdin Gurning

Reputation: 1819

I assume that You in desired output means seller and userX mean customer. So, I think this could works for you :

select data,concat('You sell product ',product_name) as logs 
from sell 
where seller = 'userid'
union all
select data,concat(customer,' put your product ',product_id,' in favourite list') 
from favorite 
where seller = 'userid'
union all
select data,concat('You have a product question') 
from question
where seller = 'userid'
order by data desc

Upvotes: 1

Related Questions