473,503 Members | 3,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL Help Please

Hello,

I would like to model and implement a simple application for keeping
track of user survey data for our marketing department.

The concept is very simple: a user of our website can opt-in to answer
a short list of questions for data collection.

The answers to all of the questions are presented in drop-down boxes,
so no free form text is stored.

We would like to store all of the questions, answer options, and user
answers in the database.

I have three simple tables to present (all fields are NOT NULL)

questions
--------------
question_id int(11)
question_text varchar(255)

answer_options
--------------
answer_option_id int(11)
question_id int(11)
answer_option_text varchar(255)

user_answers
--------------
user_answer_id int(11)
question_id int(11)
answer_option_id int(11)
user_id int(11)

What I'm scratching my brain trying to solve is how to construct the
appropriate SQL statement to, in one query, select all questions, all
answers, and those answers that a specific user_id has already
selected.

Something akin to:

SELECT q.question_id,
q.question_text,
ao.answer_option_text,
ao.answer_option_id,
ua.user_answer_id
FROM questions q, answer_options ao, user_answers ua
WHERE ????

The end result would have a not null user_answer_id where the user has
made a choice.

I have been trying with LEFT OUTER JOINS, and am thinking that a
subquery of user_answers by user_id into a temp table might be needed,
though I've never attempted one first.

Any generous folk out there mind posting up some SQL that would work?

many thanks for the help!

cheers!

Hal
Jul 19 '05 #1
3 1548
How about:

SELECT q.question_id,
q.question_text,
ao.answer_option_text,
ao.answer_option_id,
ua.user_answer_id
FROM
questions q,
answer_options ao
LEFT JOIN
user_answers ua
ON
${user_id}=ua.user_id AND ao.answer_option_id=ua.answer_option_id
WHERE
ao.question_id=q.question_id
ORDER BY
q.question_id, ao.answer_option_id

Appears to work. Any comments on improvements?

ha***********@yahoo.com (Hal Robertson) wrote in message news:<a8*************************@posting.google.c om>...
Hello,

I would like to model and implement a simple application for keeping
track of user survey data for our marketing department.

The concept is very simple: a user of our website can opt-in to answer
a short list of questions for data collection.

The answers to all of the questions are presented in drop-down boxes,
so no free form text is stored.

We would like to store all of the questions, answer options, and user
answers in the database.

I have three simple tables to present (all fields are NOT NULL)

questions
--------------
question_id int(11)
question_text varchar(255)

answer_options
--------------
answer_option_id int(11)
question_id int(11)
answer_option_text varchar(255)

user_answers
--------------
user_answer_id int(11)
question_id int(11)
answer_option_id int(11)
user_id int(11)

What I'm scratching my brain trying to solve is how to construct the
appropriate SQL statement to, in one query, select all questions, all
answers, and those answers that a specific user_id has already
selected.

Something akin to:

SELECT q.question_id,
q.question_text,
ao.answer_option_text,
ao.answer_option_id,
ua.user_answer_id
FROM questions q, answer_options ao, user_answers ua
WHERE ????

The end result would have a not null user_answer_id where the user has
made a choice.

I have been trying with LEFT OUTER JOINS, and am thinking that a
subquery of user_answers by user_id into a temp table might be needed,
though I've never attempted one first.

Any generous folk out there mind posting up some SQL that would work?

many thanks for the help!

cheers!

Hal

Jul 19 '05 #2
How about:

SELECT q.question_id,
q.question_text,
ao.answer_option_text,
ao.answer_option_id,
ua.user_answer_id
FROM
questions q,
answer_options ao
LEFT JOIN
user_answers ua
ON
${user_id}=ua.user_id AND ao.answer_option_id=ua.answer_option_id
WHERE
ao.question_id=q.question_id
ORDER BY
q.question_id, ao.answer_option_id

Appears to work. Any comments on improvements?

ha***********@yahoo.com (Hal Robertson) wrote in message news:<a8*************************@posting.google.c om>...
Hello,

I would like to model and implement a simple application for keeping
track of user survey data for our marketing department.

The concept is very simple: a user of our website can opt-in to answer
a short list of questions for data collection.

The answers to all of the questions are presented in drop-down boxes,
so no free form text is stored.

We would like to store all of the questions, answer options, and user
answers in the database.

I have three simple tables to present (all fields are NOT NULL)

questions
--------------
question_id int(11)
question_text varchar(255)

answer_options
--------------
answer_option_id int(11)
question_id int(11)
answer_option_text varchar(255)

user_answers
--------------
user_answer_id int(11)
question_id int(11)
answer_option_id int(11)
user_id int(11)

What I'm scratching my brain trying to solve is how to construct the
appropriate SQL statement to, in one query, select all questions, all
answers, and those answers that a specific user_id has already
selected.

Something akin to:

SELECT q.question_id,
q.question_text,
ao.answer_option_text,
ao.answer_option_id,
ua.user_answer_id
FROM questions q, answer_options ao, user_answers ua
WHERE ????

The end result would have a not null user_answer_id where the user has
made a choice.

I have been trying with LEFT OUTER JOINS, and am thinking that a
subquery of user_answers by user_id into a temp table might be needed,
though I've never attempted one first.

Any generous folk out there mind posting up some SQL that would work?

many thanks for the help!

cheers!

Hal

Jul 19 '05 #3
How about:

SELECT q.question_id,
q.question_text,
ao.answer_option_text,
ao.answer_option_id,
ua.user_answer_id
FROM
questions q,
answer_options ao
LEFT JOIN
user_answers ua
ON
${user_id}=ua.user_id AND ao.answer_option_id=ua.answer_option_id
WHERE
ao.question_id=q.question_id
ORDER BY
q.question_id, ao.answer_option_id

Appears to work. Any comments on improvements?

ha***********@yahoo.com (Hal Robertson) wrote in message news:<a8*************************@posting.google.c om>...
Hello,

I would like to model and implement a simple application for keeping
track of user survey data for our marketing department.

The concept is very simple: a user of our website can opt-in to answer
a short list of questions for data collection.

The answers to all of the questions are presented in drop-down boxes,
so no free form text is stored.

We would like to store all of the questions, answer options, and user
answers in the database.

I have three simple tables to present (all fields are NOT NULL)

questions
--------------
question_id int(11)
question_text varchar(255)

answer_options
--------------
answer_option_id int(11)
question_id int(11)
answer_option_text varchar(255)

user_answers
--------------
user_answer_id int(11)
question_id int(11)
answer_option_id int(11)
user_id int(11)

What I'm scratching my brain trying to solve is how to construct the
appropriate SQL statement to, in one query, select all questions, all
answers, and those answers that a specific user_id has already
selected.

Something akin to:

SELECT q.question_id,
q.question_text,
ao.answer_option_text,
ao.answer_option_id,
ua.user_answer_id
FROM questions q, answer_options ao, user_answers ua
WHERE ????

The end result would have a not null user_answer_id where the user has
made a choice.

I have been trying with LEFT OUTER JOINS, and am thinking that a
subquery of user_answers by user_id into a temp table might be needed,
though I've never attempted one first.

Any generous folk out there mind posting up some SQL that would work?

many thanks for the help!

cheers!

Hal

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2151
by: Numberwhun | last post by:
Hello everyone! I am trying to learn java and have run into kind of a snag. Here is the code that I have so far: ------ <begin_code> ---------- import javax.swing.*; import...
1
1964
by: HolaGoogle | last post by:
Hi all, Please help me with the following..it's realy urgent and i tried everything i could and i can't get it work properly!! Thanks in advance. Here's what i'm trying to accomplish: in my...
0
1964
by: s_erez | last post by:
Hi, This is a realy tricky one. I have an ASP.NET application where some pages are reading data from a DB and presenting reports. In order for the user to wait while the page is reading data from...
2
2341
by: rked | last post by:
I get nameSPAN1 is undefined when I place cursor in comments box.. <%@ LANGUAGE="VBScript" %> <% DIM ipAddress ipAddress=Request.Servervariables("REMOTE_HOST") %> <html> <head> <meta...
7
3570
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
4
3503
by: pshindle | last post by:
DB2 Team - I just downloaded and unzipped the new Fixpack 9 for DB2 ESE V8 for Windows (FP9_WR21350_ESE.exe). I then burned the unzipped Fixpack files to a CD. I proceded to install this...
23
3232
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
1
54451
PEB
by: PEB | last post by:
POSTING GUIDELINES Please follow these guidelines when posting questions Post your question in a relevant forum Do NOT PM questions to individual experts - This is not fair on them and...
4
2319
by: fatboySudsy | last post by:
Hi, I have constructed a client program that has given me some error codes that i just cannot see. I was wondering if a different set of eyes with much more experience than me could help me out. ...
0
7192
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7064
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7315
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7445
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3147
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
369
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.