0

I want to fetch crimes from crm_ms_fir table where condition should be based on category of crime database detail →

crm_ms_fir(fir_id,crime_id)
crm_ms_crime(crime_id,crime_category_id,crime_name)
crm_ms_category(category_id,category_name)    

Example: give me all fir reports where crime came under crime_category_id = 2

SELECT fir.fir_id,
       fir.crime_id,
       crm.crime_name
  FROM crm_ms_fir fir
 INNER JOIN crm_ms_crime crm
 INNER JOIN crm_ms_crime_category
    ON crm.category_id=3

This the sample query which I have written .

2
  • 3
    Are you using MySQL or Oracle? Please tag your question appropriately. Commented May 7, 2015 at 11:54
  • brother em using mysql Commented May 7, 2015 at 12:02

2 Answers 2

1

Actually you don't need to join crm_ms_crime_category because you have category_id in crm_ms_crime and don't use anything from crm_ms_crime_category. Also ON condition is missed. So try simply this:

SELECT fir.fir_id, fir.crime_id, crm.crime_name
FROM crm_ms_fir fir
INNER JOIN crm_ms_crime crm ON fir.crime_id = crm.crime_id
WHERE crm.category_id=3
Sign up to request clarification or add additional context in comments.

Comments

0

You missed the JOIN conditions, and crm_ms_category is not needed here.

SELECT fir.fir_id, fir.crime_id, crm.crime_name
FROM crm_ms_fir fir
  INNER JOIN crm_ms_crime crm ON fir.crime_id = crm.crime_id
WHERE crm.category_id=2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.