Jump to content

Help on sql query


rabesh

Recommended Posts

I have used three tables

 

tbl_question,tbl_answer,tbl_rating

i need all data of three tables

 

fileds of  tbl_question are

question_id question description user_id category_id question_slug date

 

fields of tbl_answers are

 

answer_id question_id user_id answer date

 

fields of tbl_rating are

 

rating_id answer_id point

 

for the display of one questions and answers of related question by checking from question_slug field

 

i used this query but not correct

 

plz help

 

$query= $this->db->query("(SELECT a.*,b.* FROM tbl_questions as a, tbl_answers as b where a.question_id=b.question_id AND a.question_slug='".$question_slug."') as c INNER JOIN (SELECT AVG(point) FROM tbl_rating)as c ON d.answer_id=c.answer_id ORDER BY d.answer_id DESC ")->result_array();

 

following error occurs

 

 

A Database Error Occurred

 

Error Number: 1064

 

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 c INNER JOIN (SELECT AVG(point) FROM tbl_rating)as c ON d.answer_id=c.answer_' at line 1

 

(SELECT a.*,b.* FROM tbl_questions as a, tbl_answers as b where a.question_id=b.question_id AND a.question_slug='Which-framework-is-mostly-Used-for-Php-Programming') as c INNER JOIN (SELECT AVG(point) FROM tbl_rating)as c ON d.answer_id=c.answer_id ORDER BY d.answer_id DESC

 

 

Link to comment
Share on other sites

learn how to join, and maybe learn how to write SQL while you're at it: your error message relates to the fact that you have wraped the first section of the select within parenthesis and so terminate the select prematurely, not that it would work if you removed the parenthesis either. 

 

Really, that query is a mess...It should look something more like:

SELECT 
tbl_question.question_id, question, description, user_id, category_id, question_slug, tbl_question.date AS q_date,
tbl_answer.answer_id, user_id, answer, tbl_answer.date AS a_date,
rating_id, point

FROM 
tbl_rating 
LEFT JOIN tbl_answers 
    ON (tbl_rating.answer_id = tbl_answers.answer_id)
LEFT JOIN tbl_question
    ON (tbl_question.question_id = tbl_answer.question_id)

WHERE 
tbl_question.question_slug = "$question_slug"

ORDER BY
tbl_answers.answer_id

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.