[英]Need an sql query to find results joining two tables but comparing multiple rows in second table
Below given are two sample tables 下面给出了两个示例表
Table: post 表格:发布
id title desc status
1 test testdesc active
2 test1 testdesc2 active
Table: post_meta 表格:post_meta
id post_id meta_key meta_value
1 1 _customer_user 342
2 1 date_registered 2019-03-21
3 2 _customer_user 342
4 2 date_registered 2019-03-22
I want an sql query to find those who registered on 2019-03-21 and are active. 我想要一个SQL查询来查找那些在2019-03-21注册并处于活动状态的人。 Want result like below.
想要如下结果。
id cusomerid registered_date
1 342 2019-03-21
You can try below - 您可以在下面尝试-
select a.id, b.meta_Value as customerid, b1.meta_value as registrationdate
from post a left join post_meta b
on a.id=b.post_id and b.meta_key='_customer_user'
left join post_meta b1
on a.id=b1.post_id and b1.meta_key='date_registered'
where status='active' and b1.meta_value='2019-03-21'
OUTPUT: OUTPUT:
id customerid registrationdate
1 342 2019-03-21
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.