构造一个SQL查询 [英] constructing a sql query

查看:47
本文介绍了构造一个SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在使用某些sql时遇到了麻烦,不知道我的桌面设计是不是
不正确。


我有一张代表姓名表。


我有一张代表可以回答的问题表。

然后我有一个表,每个代表包含许多行,每行

显示代表已回答的问题。


所以例如,委托id 10已回答问题1和问题7,所以

会有两行


DelegateID QuestionID

10 1

10 7




我如何编写一个选择已回答问题1的代表的查询

和问题7?


我试过select delegateID where questionID = 1 AND questionID = 7,但

当然没有记录因为没有记录满足这两个问题

ID,所以归还了。


非常感谢任何帮助。

解决方案

questionID = 1 OR questionID = 7


Daveíà?è?àa:





我有一些sql的问题,不知道我的桌面设计是不是
不正确。


我有一个代表名称表。


我有一个代表可以回答的问题表。

然后我有一个表,其中包含每个代表的许多行,每个代表行

显示代表已回答哪个问题。


例如,delegate id 10已回答问题1和问题7,所以

会有两行


DelegateID QuestionID

10 1

10 7





如何编写一个选择已回答问题的代理的查询第1页

和问题7?


我试过select delegateID where questionID = 1 AND questionID = 7,但

当然没有记录被退回,因为没有记录满足这两个问题

ID。


任何帮助将不胜感激。


我试过了,但是如果他们已经完成

这个问题会给出一个委托ID,但我只想要获得代表ID,如果他们已经完成了两个问题。


谢谢


" que" < va ****** @ gmail.com写信息

新闻:11 ********************* @ 16g2000cwy。 googlegrou ps.com ...

questionID = 1或者questionID = 7


Daveíà?è?àa:





我遇到一些sql的麻烦,不知道我的桌面设计是不是
不正确。


我有一张代表姓名表。


我有一张代表可以回答的问题表。

然后我有一个表,其中包含每个代表的许多行,每行

显示代表已回答的问题。


所以例如,delegate id 10已回答问题1和问题7,所以

会有两行


DelegateID QuestionID

10 1

10 7




我如何编写一个选择已经回答的代表的查询疑问句

1

和问题7?


我试过select delegateID where questionID = 1 AND questionID = 7,但是

当然没有记录被退回,因为没有记录满足这两个问题

ID。


任何帮助将不胜感激。





Dave写道:


我已经尝试过了,但是如果他们已经完成

这个问题,那将会给出一个代表ID,但我只想获得代表ID,如果他们已经完成

已完成两个问题。


谢谢


" que" < va ****** @ gmail.com写信息

新闻:11 ********************* @ 16g2000cwy。 googlegrou ps.com ...

questionID = 1或者questionID = 7


Daveíà?è?àa:





我遇到一些sql的麻烦,不知道我的桌面设计是不是
不正确。


我有一张代表姓名表。


我有一张代表可以回答的问题表。

然后我有一个表,其中包含每个代表的许多行,每行

显示代表已回答的问题。


所以例如,delegate id 10已回答问题1和问题7,所以

会有两行


DelegateID QuestionID

10 1

10 7




我如何编写一个选择代表的查询已回答问题

1

和问题7?


我试过select delegateID where questionID = 1 AND questionID = 7,butof

当然没有记录被退回,因为没有记录满足这两个问题

ID。


任何帮助将不胜感激。



SELECT t1 .DelegateID`

FROM mytable t1

LEFT JOIN mytable t2 ON t1 .DelegateID` = t2 .DelegateID`

WHERE t1 .QuestionID` = 1

AND t2 .QuestionID` = 7


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.

解决方案

questionID=1 OR questionID=7

Dave íà?è?àa:

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.


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 íà?è?àa:

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.




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 íà?è?àa:

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


这篇关于构造一个SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆