0

Hi everyone,i am a beginner of mysql query. I have one error when using this query in mysql:

SELECT enquiry.business_id ,enquiry_count ,b.`business_name`,c.`id`
FROM `consumer_guide_enquiry_company` enquiry
INNER JOIN `businesses` b ON  `enquiry`.`business_id`=b.`business_id`
INNER JOIN `consumer_guide_company` c ON c.`business_id`=b.`business_id` 
WHERE CASE 
WHEN enquiry.`business_id` 
IN (
SELECT b.business_id 
FROM `businesses` b,`consumer_guide_company` c 
WHERE b.`business_id`=c.`business_id` AND c.`consumer_guide_id`=1) 
THEN enquiry_count=COUNT(enquiry.enquiry_id) 
ELSE enquiry_count=0 
END
GROUP BY enquiry.business_id

It display error: Unknown column 'enquiry_count' in 'field list'

Thanks for your helping

4
  • 2
    do you have the column in any of your tables ? Apr 4, 2014 at 4:29
  • enquiry_count to enquiry.enquiry_count Apr 4, 2014 at 4:30
  • 1
    No , i don't have any enquiry_count column in any table. I don't know how to select a value is calculated in CASE WHEN THEN ElSE END expression, so i define enquiry_count value in CASE WHEN THEN ELSE END
    – beginner
    Apr 4, 2014 at 4:34
  • 1
    Why you are using SET in Where clause?
    – AK47
    Apr 4, 2014 at 4:36

2 Answers 2

0

Try Like This

SELECT enquiry.business_id ,case when enquiry_count is NULL then  0 
else enquiry_count end 'enquiry_count'  ,b.`business_name`,c.`id`
FROM `consumer_guide_enquiry_company` enquiry

INNER JOIN `businesses` b ON  `enquiry`.`business_id`=b.`business_id`
INNER JOIN `consumer_guide_company` c ON c.`business_id`=b.`business_id` 

LEFT JOIN (SELECT b.business_id,count(*) as enquiry_count FROM `businesses`
b,`consumer_guide_company` c WHERE b.`business_id`=c.`business_id` AND  
c.`consumer_guide_id`=1 group by b.business_id) as tt 
ON enquiry.`business_id`=tt.b.business_id
GROUP BY enquiry.business_id
6
  • I tried your query, but it display : "case when enquiry_count is NULL then 0 else enquiry_count end" as one column name of result.
    – beginner
    Apr 4, 2014 at 4:54
  • use the same but after end add as enquiry_count Apr 4, 2014 at 4:56
  • that is case when enquiry_count is NULL then 0 else enquiry_count end as equiry_count Apr 4, 2014 at 4:56
  • @Rat-a-tat-a-tatRatatouille: It display error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as enquiry_count'
    – beginner
    Apr 4, 2014 at 4:58
  • @Rat-a-tat-a-tatRatatouille i get above syntax in following link stackoverflow.com/questions/8600671/…
    – Sathish
    Apr 4, 2014 at 5:11
0

you should not mention the name where is tablename.enquiry_count

your code error in this following line:

wrong code line:1

SELECT enquiry.business_id ,enquiry_count ,b.`business_name`,c.`id`

wrong code line:2

    THEN enquiry_count=COUNT(enquiry.enquiry_id) 
    ELSE enquiry_count=0 

you should mention the: tablename

   tablename.enquiry_count 
3
  • 1
    I think the OP enquiry_count value set in where clause . In OP Query CASE WHEN enquiry.business_id` IN (SELECT b.business_id FROM businesses b,consumer_guide_company c WHERE b.business_id=c.business_id AND c.consumer_guide_id=1) THEN enquiry_count=COUNT(enquiry.enquiry_id) ELSE enquiry_count=0 ` So the field not in table
    – Sathish
    Apr 4, 2014 at 4:35
  • @SATSON : yes , you are right. The field is not in any table .
    – beginner
    Apr 4, 2014 at 4:36
  • 2
    @jmail how will the beginner mention tablename.enquiry_count when there is no such column in the table ??? Apr 4, 2014 at 4:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.