[英]select mysql data not belonging to the row but belong to the same order id
Trying to get a list some data that belogs to the same order_id.试图列出一些属于相同 order_id 的数据。
----- ----------- ------- -------- ------- --------- ------
ID. | order_id | f_name | l_name | phone | relation | dob |
----- ----------- ------- -------- ------- ----------- -----
1. 123. mom. smith. 123123. primary. null
2 123 dad smith. spouse null
3. 123. son. smith. son. 06282013
The data is set in this way where the order_id is the same but as yo u can see only the child has a dob.数据以这种方式设置,其中 order_id 相同,但您可以看到只有孩子有出生日期。 What I am trying to run is a mysql query where I can bring all children from age 6-12 along with the phone number from the primary
我正在尝试运行的是 mysql 查询,我可以将所有 6-12 岁的孩子连同小学的电话号码一起带上
Real Sql query.真正的 Sql 查询。 This brings the children but not the primary with the telephone number that i Need.
这给孩子们带来了我需要的电话号码,但不是主要的。
select ea.* from event_attendee ea
JOIN event_order eo on ea.order_id = eo.id
JOIN event e on eo.event_id = e.id
where e.id = '0d323c3a-33f3-4583-8b10-65100403edc2' and date_of_birth >= '2007-01-01 00:00:00' and date_of_birth <= '2020-01-2014 00:00:00' group by event_order_id;
any ideas?有任何想法吗?
Based on your sample data, this is a join:根据您的示例数据,这是一个联接:
select c.*, p.phone
from data p join
data c
on p.order_id = c.order_id and
p.relation = 'parent' and
c.relation = 'child'
where c.dob > curdate() - interval 13 year and
c.dob <= curdate() - interval 6 year;
Note: This assumes that dob
is stored properly as a date.注意:这假设
dob
正确存储为日期。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.