mysql 和 php - 在一个 select 中得到 3 个或更多表?
mysql and php - get 3 or more tables in one select?
我想向我的用户展示他最近的活动。
为此,我有 3 个 table 想要按新闻日期显示顺序。
例如:
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)
...
所以我想从不同的 tables 获取用户活动(卖家)并为他显示此警报。可能吗?任何例子如何做到这一点?
table 卖出:
id
seller
customer
product_name
data
table 最爱:
product_id
seller
customer
data
table 问题:
product_id
seller
customer
question
answer
data
我假设所需输出中的 You
表示卖家,userX
表示客户。所以,我认为这对你有用:
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
我想向我的用户展示他最近的活动。 为此,我有 3 个 table 想要按新闻日期显示顺序。
例如:
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)
...
所以我想从不同的 tables 获取用户活动(卖家)并为他显示此警报。可能吗?任何例子如何做到这一点?
table 卖出:
id
seller
customer
product_name
data
table 最爱:
product_id
seller
customer
data
table 问题:
product_id
seller
customer
question
answer
data
我假设所需输出中的 You
表示卖家,userX
表示客户。所以,我认为这对你有用:
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