mysql和php - 在一个选择中获得3个或更多表?

时间:2022-12-08 03:34:49

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.

我想向我的用户展示他最后的活动。为此,我有3个表,我想按日期显示新闻。

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

1 个解决方案

#1


1  

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

我假设您在所需的输出中意味着卖方和用户X意味着客户。所以,我认为这可能对你有用:

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

#1


1  

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

我假设您在所需的输出中意味着卖方和用户X意味着客户。所以,我认为这可能对你有用:

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