简体   繁体   English

需要一个sql查询来查找连接两个表但比较第二个表中的多行的结果

[英]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 - 您可以在下面尝试-

DEMO DEMO

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.

相关问题 连接两个表并显示第二个表行数据,用单个查询的第一个表行用逗号分隔 - Joining two tables and display second table row data comma seperated for first table rows using single query 优化 SQL 查询 - 从第二个表中具有多行的两个表中获取记录 - Optimize SQL Query - Fetch records from two tables with multiple rows in second table 连接两个表以显示第二个表中的行数 - Joining two tables to display count of rows in second table SQL 查询:连接两个表,其中第一个表的实体没有,或另一个表中有多个条目 - SQL Query: Joining two tables where entity of the first table has no, or multiple entries in other table 用于联接包含多个行的多个表的SQL查询-正确的方法吗? - SQL Query for joining multiple tables containing multiple rows - The right way? MySQL查询问题,左连接表与其他两个表的结果 - MySQL query issue with left joining a table with the results of two other tables 连接两个表并选择第二个中不存在的行1 - Joining two tables and selecting rows not present in the second 1 需要一个SQL查询以通过在Codeigniter中联接两个表来获取值 - Need a SQL query to get a value by joining two table in codeigniter MYSQL:查询两个表并将第二个表中的结果连接到一个数组 - MYSQL: Query two tables and join results from second table to an array 用COALESCE连接SQL表中的3个表不返回结果 - Joining 3 tables in SQL table with COALESCE returning no results
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM