Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

mysql - is there a way to display only child if it exists or else only parent data

I have two tables and one is parent table and second one is child table both tables has transactions but where as some transactions will take place in parent table and some in child based on their child existence if the parent row doesn't have any child it will be the result and if the parent has the child the child table itself will be the result some times both may occur i tried but not getting the exact solution please help.....

1st table and Parent table


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
SELECT IFNULL(child.item, parent.item) AS item, 
       IFNULL(child.amount, parent.amount) AS amount,
       IFNULL(child.tax, parent.tax) AS tax
FROM parent
LEFT JOIN child ON parent.id = child.p_id

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...