admin管理员组文章数量:1293693
We have a simple table with the following columns:
- userid (unique key)
- name
- dateofbirth
- parentid
If the user is a minor, then parentid contains the userid of the parent. Otherwise it is NULL.
Need to generate a list of all parents(adults) with their children (minors). One parent may have multiple children. Each person has a unique userid - unique key
in the table.
We have approx 50K records. So it's not feasible to do it manually.
Hopefully it is not too complicated for an expert. Greatly appreciate any help!! TIA
I tried the following:
SELECT uu.userid, uu.name, uu.dateofbirth, uu.parentid
FROM usertable uu
WHERE uu.userid = (SELECT pp.userid
FROM usertable pp
WHERE pp.userid = uu.parentid);
It generates one row which is not what I need. I need the following:
Example table contents: enter image description here
Then my query should fetch all records except the first one. The first one has an adult with no children - I don't need that. I need all parents and their children only.
本文标签: A simple SQL query for MYSQL is not workingStack Overflow
版权声明:本文标题:A simple SQL query for MYSQL is not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741580506a2386544.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论