473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

constructing a sql query

Hi

I'm having trouble with some sql and don't know if my table design is
incorrect.

I have a table of delegate names.

I have a table of possible questions the delegate can answer.

I then have a table which contains many rows for each delegate, each row
showing which question the delegate has answered.

So for example, delegate id 10 has answered question 1 and question 7, so
there would be two rows

DelegateID QuestionID
10 1
10 7

etc.

How do i write a query that selects delegates that have answered question 1
AND question 7?

I tried "select delegateID where questionID=1 AND questionID=7, but of
course no records were returned since no records satisfied both question
IDs.

Any help would be greatly appreciated.

Dec 12 '06 #1
16 2407
que
questionID=1 OR questionID=7

Dave написав:
Hi

I'm having trouble with some sql and don't know if my table design is
incorrect.

I have a table of delegate names.

I have a table of possible questions the delegate can answer.

I then have a table which contains many rows for each delegate, each row
showing which question the delegate has answered.

So for example, delegate id 10 has answered question 1 and question 7, so
there would be two rows

DelegateID QuestionID
10 1
10 7

etc.

How do i write a query that selects delegates that have answered question1
AND question 7?

I tried "select delegateID where questionID=1 AND questionID=7, but of
course no records were returned since no records satisfied both question
IDs.

Any help would be greatly appreciated.
Dec 12 '06 #2
I have tried that, but that will give a delegate id if they have completed
either question, but i only want to get the delegate id if they have
completed BOTH questions.

Thanks

"que" <va******@gmail.comwrote in message
news:11*********************@16g2000cwy.googlegrou ps.com...
questionID=1 OR questionID=7

Dave написав:
Hi

I'm having trouble with some sql and don't know if my table design is
incorrect.

I have a table of delegate names.

I have a table of possible questions the delegate can answer.

I then have a table which contains many rows for each delegate, each row
showing which question the delegate has answered.

So for example, delegate id 10 has answered question 1 and question 7, so
there would be two rows

DelegateID QuestionID
10 1
10 7

etc.

How do i write a query that selects delegates that have answered question
1
AND question 7?

I tried "select delegateID where questionID=1 AND questionID=7, but of
course no records were returned since no records satisfied both question
IDs.

Any help would be greatly appreciated.

Dec 12 '06 #3

Dave wrote:
I have tried that, but that will give a delegate id if they have completed
either question, but i only want to get the delegate id if they have
completed BOTH questions.

Thanks

"que" <va******@gmail.comwrote in message
news:11*********************@16g2000cwy.googlegrou ps.com...
questionID=1 OR questionID=7

Dave написав:
Hi

I'm having trouble with some sql and don't know if my table design is
incorrect.

I have a table of delegate names.

I have a table of possible questions the delegate can answer.

I then have a table which contains many rows for each delegate, each row
showing which question the delegate has answered.

So for example, delegate id 10 has answered question 1 and question 7, so
there would be two rows

DelegateID QuestionID
10 1
10 7

etc.

How do i write a query that selects delegates that have answered question
1
AND question 7?

I tried "select delegateID where questionID=1 AND questionID=7, butof
course no records were returned since no records satisfied both question
IDs.

Any help would be greatly appreciated.
SELECT t1.`DelegateID`
FROM mytable t1
LEFT JOIN mytable t2 ON t1.`DelegateID` = t2.`DelegateID`
WHERE t1.`QuestionID` = 1
AND t2.`QuestionID` = 7

Dec 12 '06 #4
Thanks for the reply. but i can't make this work since questionID does not
exist in t1
"strawberry" <za*******@gmail.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...

Dave wrote:
I have tried that, but that will give a delegate id if they have completed
either question, but i only want to get the delegate id if they have
completed BOTH questions.

Thanks

"que" <va******@gmail.comwrote in message
news:11*********************@16g2000cwy.googlegrou ps.com...
questionID=1 OR questionID=7

Dave написав:
Hi

I'm having trouble with some sql and don't know if my table design is
incorrect.

I have a table of delegate names.

I have a table of possible questions the delegate can answer.

I then have a table which contains many rows for each delegate, each row
showing which question the delegate has answered.

So for example, delegate id 10 has answered question 1 and question 7,
so
there would be two rows

DelegateID QuestionID
10 1
10 7

etc.

How do i write a query that selects delegates that have answered
question
1
AND question 7?

I tried "select delegateID where questionID=1 AND questionID=7, but of
course no records were returned since no records satisfied both question
IDs.

Any help would be greatly appreciated.
SELECT t1.`DelegateID`
FROM mytable t1
LEFT JOIN mytable t2 ON t1.`DelegateID` = t2.`DelegateID`
WHERE t1.`QuestionID` = 1
AND t2.`QuestionID` = 7
Dec 12 '06 #5

Dave wrote:
Thanks for the reply. but i can't make this work since questionID does not
exist in t1
"strawberry" <za*******@gmail.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...

Dave wrote:
I have tried that, but that will give a delegate id if they have completed
either question, but i only want to get the delegate id if they have
completed BOTH questions.

Thanks

"que" <va******@gmail.comwrote in message
news:11*********************@16g2000cwy.googlegrou ps.com...
questionID=1 OR questionID=7

Dave написав:
Hi
>
I'm having trouble with some sql and don't know if my table design is
incorrect.
>
I have a table of delegate names.
>
I have a table of possible questions the delegate can answer.
>
I then have a table which contains many rows for each delegate, each row
showing which question the delegate has answered.
>
So for example, delegate id 10 has answered question 1 and question 7,
so
there would be two rows
>
DelegateID QuestionID
10 1
10 7
>
etc.
>
How do i write a query that selects delegates that have answered
question
1
AND question 7?
>
I tried "select delegateID where questionID=1 AND questionID=7, but of
course no records were returned since no records satisfied both question
IDs.
>
Any help would be greatly appreciated.

SELECT t1.`DelegateID`
FROM mytable t1
LEFT JOIN mytable t2 ON t1.`DelegateID` = t2.`DelegateID`
WHERE t1.`QuestionID` = 1
AND t2.`QuestionID` = 7

???

What's the table with the question ids called?

Dec 12 '06 #6

strawberry wrote:
Dave wrote:
Thanks for the reply. but i can't make this work since questionID does not
exist in t1
"strawberry" <za*******@gmail.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...

Dave wrote:
I have tried that, but that will give a delegate id if they have completed
either question, but i only want to get the delegate id if they have
completed BOTH questions.
>
Thanks
>
"que" <va******@gmail.comwrote in message
news:11*********************@16g2000cwy.googlegrou ps.com...
questionID=1 OR questionID=7
>
Dave написав:
Hi

I'm having trouble with some sql and don't know if my table design is
incorrect.

I have a table of delegate names.

I have a table of possible questions the delegate can answer.

I then have a table which contains many rows for each delegate, each row
showing which question the delegate has answered.

So for example, delegate id 10 has answered question 1 and question7,
so
there would be two rows

DelegateID QuestionID
10 1
10 7

etc.

How do i write a query that selects delegates that have answered
question
1
AND question 7?

I tried "select delegateID where questionID=1 AND questionID=7,but of
course no records were returned since no records satisfied both question
IDs.

Any help would be greatly appreciated.
SELECT t1.`DelegateID`
FROM mytable t1
LEFT JOIN mytable t2 ON t1.`DelegateID` = t2.`DelegateID`
WHERE t1.`QuestionID` = 1
AND t2.`QuestionID` = 7


???

What's the table with the question ids called?
I mean this table:

DelegateID QuestionID
10 1
10 7

Dec 12 '06 #7
On Dec 12, 8:49 am, "Dave" <d...@nospam.co.ukwrote:
Thanks for the reply. but i can't make this work since questionID does not
exist in t1

"strawberry" <zac.ca...@gmail.comwrote in messagenews:1165930145.656407..31****@l12g2000cwl. googlegroups.com...

Dave wrote:
I have tried that, but that will give a delegate id if they have completed
either question, but i only want to get the delegate id if they have
completed BOTH questions.
Thanks
"que" <varyf...@gmail.comwrote in message
news:11*********************@16g2000cwy.googlegrou ps.com...
questionID=1 OR questionID=7
Dave написав:
Hi
I'm having trouble with some sql and don't know if my table design is
incorrect.
I have a table of delegate names.
I have a table of possible questions the delegate can answer.
I then have a table which contains many rows for each delegate, each row
showing which question the delegate has answered.
So for example, delegate id 10 has answered question 1 and question 7,
so
there would be two rows
DelegateID QuestionID
10 1
10 7
etc.
How do i write a query that selects delegates that have answered
question
1
AND question 7?
I tried "select delegateID where questionID=1 AND questionID=7, but of
course no records were returned since no records satisfied both question
IDs.
Any help would be greatly appreciated.SELECT t1.`DelegateID`
FROM mytable t1
LEFT JOIN mytable t2 ON t1.`DelegateID` = t2.`DelegateID`
WHERE t1.`QuestionID` = 1
AND t2.`QuestionID` = 7
Either of the following work?

-- ...if you're using a MySQL version without subselects
SELECT d1.DelegateID
FROM delegates d1
LEFT JOIN questions q1 ON q1.DelegateID=d1.DelegateID AND
q1.QuestionID=1
LEFT JOIN questions q2 ON q2.DelegateID=d1.DelegateID AND
q2.QuestionID=7 ;

-- ...if you're using a MySQL version with subselects
SELECT d.DelegateID
FROM delegates d
WHERE EXISTS (
SELECT 1 FROM questions q
WHERE q.DelegateID=d.DelegateID
AND q.QuestionID=1
)
AND EXISTS (
SELECT 1 FROM questions q
WHERE q.DelegateID=d.DelegateID
AND q.QuestionID=7
) ;

Dec 12 '06 #8
On Dec 12, 9:16 am, "shakahsha...@gmail.com" <shakahsha...@gmail.com>
wrote:
On Dec 12, 8:49 am, "Dave" <d...@nospam.co.ukwrote:
Thanks for the reply. but i can't make this work since questionID does not
exist in t1
"strawberry" <zac.ca...@gmail.comwrote in messagenews:11**********************@l12g2000cwl.g ooglegroups.com...
Dave wrote:
I have tried that, but that will give a delegate id if they have completed
either question, but i only want to get the delegate id if they have
completed BOTH questions.
Thanks
"que" <varyf...@gmail.comwrote in message
>news:11*********************@16g2000cwy.googlegro ups.com...
questionID=1 OR questionID=7
Dave написав:
Hi
I'm having trouble with some sql and don't know if my table design is
incorrect.
I have a table of delegate names.
I have a table of possible questions the delegate can answer.
I then have a table which contains many rows for each delegate, each row
showing which question the delegate has answered.
So for example, delegate id 10 has answered question 1 and question7,
so
there would be two rows
DelegateID QuestionID
10 1
10 7
etc.
How do i write a query that selects delegates that have answered
question
1
AND question 7?
I tried "select delegateID where questionID=1 AND questionID=7,but of
course no records were returned since no records satisfied both question
IDs.
Any help would be greatly appreciated.SELECT t1.`DelegateID`
FROM mytable t1
LEFT JOIN mytable t2 ON t1.`DelegateID` = t2.`DelegateID`
WHERE t1.`QuestionID` = 1
AND t2.`QuestionID` = 7Either of the following work?

-- ...if you're using a MySQL version without subselects
SELECT d1.DelegateID
FROM delegates d1
LEFT JOIN questions q1 ON q1.DelegateID=d1.DelegateID AND
q1.QuestionID=1
LEFT JOIN questions q2 ON q2.DelegateID=d1.DelegateID AND
q2.QuestionID=7 ;

-- ...if you're using a MySQL version with subselects
SELECT d.DelegateID
FROM delegates d
WHERE EXISTS (
SELECT 1 FROM questions q
WHERE q.DelegateID=d.DelegateID
AND q.QuestionID=1
)
AND EXISTS (
SELECT 1 FROM questions q
WHERE q.DelegateID=d.DelegateID
AND q.QuestionID=7
) ;
-- ...or, for that matter
SELECT q1.DelegateID
FROM questions q1
INNER JOIN questions q2
ON q2.DelegateID=q1.DelegateID
AND q2.QuestionID=7
WHERE q1.QuestionID=1 ;

SELECT q1.DelegateID
FROM questions q1
WHERE q1.QuestionID=1
AND EXISTS (
SELECT 1 FROM questions q2
WHERE q2.DelegateID=q1.DelegateID
AND q2.QuestionID=7
) ;

Dec 12 '06 #9
que
other, little bit tricky solution :)

select DelegateID, count(DelegateID) c from your_table where QuestionID
in (1, 7)
group by DelegateID
having c=2
c=2 -- 2 - params count inside in()

eg: where QuestionID in (1, 2, 3, 4, 5) -- c=5

PS: one requirement - pair delegate-question must be uniq

Dec 12 '06 #10
Thanks for staying with this one,

i have the delegate name table as t1, the question definitions table as t2,
and the joining table is now called mytable and that lists delegates and the
questions they have answered, but still no joy.

I don't get any data returned.

I also don't understand why you have

WHERE t1.`QuestionID` = 1 AND t2.`QuestionID` = 7

why does this reference t1 and t2?

"strawberry" <za*******@gmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...

strawberry wrote:
Dave wrote:
Thanks for the reply. but i can't make this work since questionID does
not
exist in t1
"strawberry" <za*******@gmail.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...

Dave wrote:
I have tried that, but that will give a delegate id if they have
completed
either question, but i only want to get the delegate id if they have
completed BOTH questions.
>
Thanks
>
"que" <va******@gmail.comwrote in message
news:11*********************@16g2000cwy.googlegrou ps.com...
questionID=1 OR questionID=7
>
Dave написав:
Hi

I'm having trouble with some sql and don't know if my table design
is
incorrect.

I have a table of delegate names.

I have a table of possible questions the delegate can answer.

I then have a table which contains many rows for each delegate, each
row
showing which question the delegate has answered.

So for example, delegate id 10 has answered question 1 and question
7,
so
there would be two rows

DelegateID QuestionID
10 1
10 7

etc.

How do i write a query that selects delegates that have answered
question
1
AND question 7?

I tried "select delegateID where questionID=1 AND questionID=7, but
of
course no records were returned since no records satisfied both
question
IDs.

Any help would be greatly appreciated.
SELECT t1.`DelegateID`
FROM mytable t1
LEFT JOIN mytable t2 ON t1.`DelegateID` = t2.`DelegateID`
WHERE t1.`QuestionID` = 1
AND t2.`QuestionID` = 7


???

What's the table with the question ids called?
I mean this table:

DelegateID QuestionID
10 1
10 7
Dec 12 '06 #11

Dave wrote:
Thanks for staying with this one,

i have the delegate name table as t1, the question definitions table as t2,
and the joining table is now called mytable and that lists delegates and the
questions they have answered, but still no joy.

I don't get any data returned.

I also don't understand why you have

WHERE t1.`QuestionID` = 1 AND t2.`QuestionID` = 7

why does this reference t1 and t2?

"strawberry" <za*******@gmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...

strawberry wrote:
Dave wrote:
Thanks for the reply. but i can't make this work since questionID does
not
exist in t1
>
>
"strawberry" <za*******@gmail.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...
>
Dave wrote:
>
I have tried that, but that will give a delegate id if they have
completed
either question, but i only want to get the delegate id if they have
completed BOTH questions.

Thanks

"que" <va******@gmail.comwrote in message
news:11*********************@16g2000cwy.googlegrou ps.com...
questionID=1 OR questionID=7

Dave написав:
Hi
>
I'm having trouble with some sql and don't know if my table design
is
incorrect.
>
I have a table of delegate names.
>
I have a table of possible questions the delegate can answer.
>
I then have a table which contains many rows for each delegate, each
row
showing which question the delegate has answered.
>
So for example, delegate id 10 has answered question 1 and question
7,
so
there would be two rows
>
DelegateID QuestionID
10 1
10 7
>
etc.
>
How do i write a query that selects delegates that have answered
question
1
AND question 7?
>
I tried "select delegateID where questionID=1 AND questionID=7, but
of
course no records were returned since no records satisfied both
question
IDs.
>
Any help would be greatly appreciated.
>
SELECT t1.`DelegateID`
FROM mytable t1
LEFT JOIN mytable t2 ON t1.`DelegateID` = t2.`DelegateID`
WHERE t1.`QuestionID` = 1
AND t2.`QuestionID` = 7

???

What's the table with the question ids called?

I mean this table:

DelegateID QuestionID
10 1
10 7
Try to avoid 'top-posting' (putting your reply after the respondent).

t1 and t2 both refer to the same table - the 'questions_delegates'
table in this case - or whatever it is you've call it. The delegates
table is redundant for the purposes of this query.

Dec 12 '06 #12

strawberry wrote:
Dave wrote:
Thanks for staying with this one,

i have the delegate name table as t1, the question definitions table ast2,
and the joining table is now called mytable and that lists delegates and the
questions they have answered, but still no joy.

I don't get any data returned.

I also don't understand why you have

WHERE t1.`QuestionID` = 1 AND t2.`QuestionID` = 7

why does this reference t1 and t2?

"strawberry" <za*******@gmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...

strawberry wrote:
Dave wrote:
>
Thanks for the reply. but i can't make this work since questionID does
not
exist in t1


"strawberry" <za*******@gmail.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...

Dave wrote:

I have tried that, but that will give a delegate id if they have
completed
either question, but i only want to get the delegate id if they have
completed BOTH questions.
>
Thanks
>
"que" <va******@gmail.comwrote in message
news:11*********************@16g2000cwy.googlegrou ps.com...
questionID=1 OR questionID=7
>
Dave написав:
Hi

I'm having trouble with some sql and don't know if my table design
is
incorrect.

I have a table of delegate names.

I have a table of possible questions the delegate can answer.

I then have a table which contains many rows for each delegate,each
row
showing which question the delegate has answered.

So for example, delegate id 10 has answered question 1 and question
7,
so
there would be two rows

DelegateID QuestionID
10 1
10 7

etc.

How do i write a query that selects delegates that have answered
question
1
AND question 7?

I tried "select delegateID where questionID=1 AND questionID=7, but
of
course no records were returned since no records satisfied both
question
IDs.

Any help would be greatly appreciated.

SELECT t1.`DelegateID`
FROM mytable t1
LEFT JOIN mytable t2 ON t1.`DelegateID` = t2.`DelegateID`
WHERE t1.`QuestionID` = 1
AND t2.`QuestionID` = 7
>
>
???
>
What's the table with the question ids called?
I mean this table:

DelegateID QuestionID
10 1
10 7

Try to avoid 'top-posting' (putting your reply after the respondent).

t1 and t2 both refer to the same table - the 'questions_delegates'
table in this case - or whatever it is you've call it. The delegates
table is redundant for the purposes of this query.
Try to avoid 'top-posting' (putting your reply after the respondent).
Doh. Well, you know what I meant!

Dec 12 '06 #13

"strawberry" <za*******@gmail.comwrote in message
news:11**********************@f1g2000cwa.googlegro ups.com...

strawberry wrote:
Dave wrote:
Thanks for staying with this one,

i have the delegate name table as t1, the question definitions table as
t2,
and the joining table is now called mytable and that lists delegates and
the
questions they have answered, but still no joy.

I don't get any data returned.

I also don't understand why you have

WHERE t1.`QuestionID` = 1 AND t2.`QuestionID` = 7

why does this reference t1 and t2?

"strawberry" <za*******@gmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...

strawberry wrote:
Dave wrote:
>
Thanks for the reply. but i can't make this work since questionID
does
not
exist in t1


"strawberry" <za*******@gmail.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...

Dave wrote:

I have tried that, but that will give a delegate id if they have
completed
either question, but i only want to get the delegate id if they
have
completed BOTH questions.
>
Thanks
>
"que" <va******@gmail.comwrote in message
news:11*********************@16g2000cwy.googlegrou ps.com...
questionID=1 OR questionID=7
>
Dave написав:
Hi

I'm having trouble with some sql and don't know if my table
design
is
incorrect.

I have a table of delegate names.

I have a table of possible questions the delegate can answer.

I then have a table which contains many rows for each delegate,
each
row
showing which question the delegate has answered.

So for example, delegate id 10 has answered question 1 and
question
7,
so
there would be two rows

DelegateID QuestionID
10 1
10 7

etc.

How do i write a query that selects delegates that have answered
question
1
AND question 7?

I tried "select delegateID where questionID=1 AND questionID=7,
but
of
course no records were returned since no records satisfied both
question
IDs.

Any help would be greatly appreciated.

SELECT t1.`DelegateID`
FROM mytable t1
LEFT JOIN mytable t2 ON t1.`DelegateID` = t2.`DelegateID`
WHERE t1.`QuestionID` = 1
AND t2.`QuestionID` = 7
>
>
???
>
What's the table with the question ids called?
I mean this table:

DelegateID QuestionID
10 1
10 7

Try to avoid 'top-posting' (putting your reply after the respondent).

t1 and t2 both refer to the same table - the 'questions_delegates'
table in this case - or whatever it is you've call it. The delegates
table is redundant for the purposes of this query.
Try to avoid 'top-posting' (putting your reply after the respondent).
Doh. Well, you know what I meant!
Ok, i seem to have that working now (although not exactly sure how that
works), but how is that now expandable to make it work with multiple
question, i.e to get delegates that have answered questions 1,2,3,7,8,10?

Thanks for your help

Dec 12 '06 #14

Dave wrote:
"strawberry" <za*******@gmail.comwrote in message
news:11**********************@f1g2000cwa.googlegro ups.com...

strawberry wrote:
Dave wrote:
Thanks for staying with this one,
>
i have the delegate name table as t1, the question definitions table as
t2,
and the joining table is now called mytable and that lists delegates and
the
questions they have answered, but still no joy.
>
I don't get any data returned.
>
I also don't understand why you have
>
WHERE t1.`QuestionID` = 1 AND t2.`QuestionID` = 7
>
why does this reference t1 and t2?
>
>
>
"strawberry" <za*******@gmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
>
strawberry wrote:
>
Dave wrote:

Thanks for the reply. but i can't make this work since questionID
does
not
exist in t1
>
>
"strawberry" <za*******@gmail.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...
>
Dave wrote:
>
I have tried that, but that will give a delegate id if they have
completed
either question, but i only want to get the delegate id if they
have
completed BOTH questions.

Thanks

"que" <va******@gmail.comwrote in message
news:11*********************@16g2000cwy.googlegrou ps.com...
questionID=1 OR questionID=7

Dave написав:
Hi
>
I'm having trouble with some sql and don't know if my table
design
is
incorrect.
>
I have a table of delegate names.
>
I have a table of possible questions the delegate can answer.
>
I then have a table which contains many rows for each delegate,
each
row
showing which question the delegate has answered.
>
So for example, delegate id 10 has answered question 1 and
question
7,
so
there would be two rows
>
DelegateID QuestionID
10 1
10 7
>
etc.
>
How do i write a query that selects delegates that have answered
question
1
AND question 7?
>
I tried "select delegateID where questionID=1 AND questionID=7,
but
of
course no records were returned since no records satisfied both
question
IDs.
>
Any help would be greatly appreciated.
>
SELECT t1.`DelegateID`
FROM mytable t1
LEFT JOIN mytable t2 ON t1.`DelegateID` = t2.`DelegateID`
WHERE t1.`QuestionID` = 1
AND t2.`QuestionID` = 7


???

What's the table with the question ids called?
>
I mean this table:
>
DelegateID QuestionID
10 1
10 7
Try to avoid 'top-posting' (putting your reply after the respondent).

t1 and t2 both refer to the same table - the 'questions_delegates'
table in this case - or whatever it is you've call it. The delegates
table is redundant for the purposes of this query.
Try to avoid 'top-posting' (putting your reply after the respondent).
Doh. Well, you know what I meant!

Ok, i seem to have that working now (although not exactly sure how that
works), but how is that now expandable to make it work with multiple
question, i.e to get delegates that have answered questions 1,2,3,7,8,10?

Thanks for your help
Yes - but this might not be the most efficient way to do it. I may be
mistaken but my guess is that the query gets exponentially slower for
every additional condition. Anyhow the syntax would look like this:

I've added a few 'AS's - just to make it a bit clearer.

SELECT t1.`ID`
FROM mytable AS t1
LEFT JOIN mytable AS t2 ON t1.`TASK-ID` = t2.`TASK-ID`
LEFT JOIN mytable AS t3 ON t1.`TASK-ID` = t3.`TASK-ID`
....
WHERE t1.`QuestionID` =4
AND t2.`QuestionID` =5
AND t3.`QuestionID` =6
....

Dec 12 '06 #15
This worked well, thank you

"que" <va******@gmail.comwrote in message
news:11**********************@80g2000cwy.googlegro ups.com...
other, little bit tricky solution :)

select DelegateID, count(DelegateID) c from your_table where QuestionID
in (1, 7)
group by DelegateID
having c=2
c=2 -- 2 - params count inside in()

eg: where QuestionID in (1, 2, 3, 4, 5) -- c=5

PS: one requirement - pair delegate-question must be uniq

Dec 12 '06 #16

"strawberry" <za*******@gmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...

Dave wrote:
"strawberry" <za*******@gmail.comwrote in message
news:11**********************@f1g2000cwa.googlegro ups.com...

strawberry wrote:
Dave wrote:
Thanks for staying with this one,
>
i have the delegate name table as t1, the question definitions table
as
t2,
and the joining table is now called mytable and that lists delegates
and
the
questions they have answered, but still no joy.
>
I don't get any data returned.
>
I also don't understand why you have
>
WHERE t1.`QuestionID` = 1 AND t2.`QuestionID` = 7
>
why does this reference t1 and t2?
>
>
>
"strawberry" <za*******@gmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
>
strawberry wrote:
>
Dave wrote:

Thanks for the reply. but i can't make this work since questionID
does
not
exist in t1
>
>
"strawberry" <za*******@gmail.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...
>
Dave wrote:
>
I have tried that, but that will give a delegate id if they have
completed
either question, but i only want to get the delegate id if they
have
completed BOTH questions.

Thanks

"que" <va******@gmail.comwrote in message
news:11*********************@16g2000cwy.googlegrou ps.com...
questionID=1 OR questionID=7

Dave написав:
Hi
>
I'm having trouble with some sql and don't know if my table
design
is
incorrect.
>
I have a table of delegate names.
>
I have a table of possible questions the delegate can answer.
>
I then have a table which contains many rows for each
delegate,
each
row
showing which question the delegate has answered.
>
So for example, delegate id 10 has answered question 1 and
question
7,
so
there would be two rows
>
DelegateID QuestionID
10 1
10 7
>
etc.
>
How do i write a query that selects delegates that have
answered
question
1
AND question 7?
>
I tried "select delegateID where questionID=1 AND
questionID=7,
but
of
course no records were returned since no records satisfied
both
question
IDs.
>
Any help would be greatly appreciated.
>
SELECT t1.`DelegateID`
FROM mytable t1
LEFT JOIN mytable t2 ON t1.`DelegateID` = t2.`DelegateID`
WHERE t1.`QuestionID` = 1
AND t2.`QuestionID` = 7


???

What's the table with the question ids called?
>
I mean this table:
>
DelegateID QuestionID
10 1
10 7
Try to avoid 'top-posting' (putting your reply after the respondent).

t1 and t2 both refer to the same table - the 'questions_delegates'
table in this case - or whatever it is you've call it. The delegates
table is redundant for the purposes of this query.
Try to avoid 'top-posting' (putting your reply after the respondent).
Doh. Well, you know what I meant!

Ok, i seem to have that working now (although not exactly sure how that
works), but how is that now expandable to make it work with multiple
question, i.e to get delegates that have answered questions 1,2,3,7,8,10?

Thanks for your help
Yes - but this might not be the most efficient way to do it. I may be
mistaken but my guess is that the query gets exponentially slower for
every additional condition. Anyhow the syntax would look like this:

I've added a few 'AS's - just to make it a bit clearer.

SELECT t1.`ID`
FROM mytable AS t1
LEFT JOIN mytable AS t2 ON t1.`TASK-ID` = t2.`TASK-ID`
LEFT JOIN mytable AS t3 ON t1.`TASK-ID` = t3.`TASK-ID`
....
WHERE t1.`QuestionID` =4
AND t2.`QuestionID` =5
AND t3.`QuestionID` =6
....
Thanks for all of your help, the problem is now solved
Dec 12 '06 #17

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

Similar topics

8
by: Benjamin Scott | last post by:
Hello. I attempted to build a compound dictionary: len(Lst)=1000 len(nuerLst)=250 len(nuestLst)=500 Dict={}
3
by: karthick | last post by:
Hi, I am constructing a Message (Body) for sending our Emails. It is around 3000 characters long. But for whatever reason, the last line seems to be broken with a "!" exclamatory mark in it,...
2
by: Alex Martinoff | last post by:
I'm wondering if it's possible to have a query construct a column where the value of the column at each row is taken from another field in that same row. For example, suppose you have a table like:...
7
by: Hyoung Lee | last post by:
A simple method of simulating a class, such as C++ or UML class, in C would be using the struct construct. Similarly, we use C functions to simulate a methods; e.g., rv method_a (&struct,...
1
by: lauren quantrell | last post by:
I have a table of contacts tblCon that includes customers and sales persons with the identity column ConID I have a table tblLinks that contains links between contacts. It looks like this: ID ...
3
by: Gary | last post by:
I have a DataSet where two tables have a relation (the parent / child relationship is also successfully added to the DataSet Relations collection). I am looking for a way to programmatically...
2
by: mel waite | last post by:
I want to create a database using Microsoft access 2003 that is relatively straightforward; it is for a new club that I'm starting. It is quite as small club with only about 100 members maximum, I...
13
by: salzan | last post by:
I developed a query in query window and copied the sql statement as follows in my code... strSQL = "SELECT tblCategory.CatName, " & _ "tblDepartment.DeptName, " & _ ...
0
by: nagar | last post by:
I'm using SQLite with the ADO.NET 2.0 SQLite Data Provider as the storage of my application. I chose to use an untyped dataset and I'm using a dataadapter to report changes to the DB. The query...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...

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.