I have table A and B.
Table A has data like
| id | status | made at | ... |
| 1 | new |14-04-14 | ... |
| 2 | fixed |14-08-12 | ... |
| 3 | fixed |14-03-15 | ... |
| ... | ... | .. | ... |
and in Table B,
| id | A_id | changes |changed at| ... |
| 1 | 1 | new | 14-04-14 | ... |
| 2 | 2 | new | 14-08-12 | ... |
| 3 | 2 | fixed | 14-08-28 | ... |
| 4 | 3 | new | 14-03-15 | ... |
| 5 | 3 | fixed | 14-05-11 | ... |
| 6 | 3 | fixed | 14-05-14 | ... |
| .. | .. | .. | .. | ... |
What I want as the result is to pick what has fixed status in table A and fixed changes in table B, with no repetition
.
If it has same changes
like 5 and 6 in B, I will pick only changed at latest data.
So the result will look like...
| id | A_id | made at | status | changes | changed at | ... |
| 2 | 2 | 14-08-12 | fixed | fixed | 14-08-28 | ... |
| 3 | 3 | 14-03-15 | fixed | fixed | 14-05-14 | ... |
I tried select * from A, B where (A.status='fixed') and (A.id=B.A_id) and (B.changes='fixed')
but still have repetition result in changes.
How can I make query right?
Try This Query
SELECT tab.* FROM(SELECT A.*,B.A_id,B.chnages,B.changed at
FROM A LEFT JOIN B ON A.id=B.A_id
WHERE A.status='fixed' AND B.changes ='fixed'
ORDER BY B.changes_at DESC ) as tab GROUP BY tab.id
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments