Jump to content

Join Multiple table


Go to solution Solved by Barand,

Recommended Posts

Hi Guys, i have a problem regarding on joining multiple tables, i was trying up for few days, but still can't get it done. This database is to join employee survey question regarding to one of the cafe. 

 

Below are my current non-working query.

SELECT question.*, question_options.*, answer.*,txtanswer.*
FROM question
    LEFT JOIN question_options
        ON question.question_id = question_options.question_id
    LEFT JOIN answer
        ON answer.option_id = question_options.qtn_option_id
	 JOIN txtanswer
		ON txtanswer.qtn_option_id= question_options.qtn_option_id
		WHERE answer.empid = 'EMP8969' 

This query show no result when i join to the txtanswer table.

Please download the attachment to view my table data. 

 

Thanks for your help.

Jointable.zip

Link to comment
https://forums.phpfreaks.com/topic/301939-join-multiple-table/
Share on other sites

  • Solution

I'd make a couple of changes

 

  1. move the WHERE condition to the JOIN condition for the answer table
  2. use a LEFT join for the txtanswer table
SELECT question.*, question_options.*, answer.*,txtanswer.*
FROM question
    LEFT JOIN question_options
        ON question.question_id = question_options.question_id
    LEFT JOIN answer
        ON answer.option_id = question_options.qtn_option_id
           AND answer.empid = 'EMP8969' 
	 LEFT JOIN txtanswer
		ON txtanswer.qtn_option_id= question_options.qtn_option_id
		 

And don't use *s in the SELECT, specify the required fields.

Link to comment
https://forums.phpfreaks.com/topic/301939-join-multiple-table/#findComment-1536332
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.