在MySql数据库中有AM_COURSE
UserId CourseName course_desc
101 java sometext...
102 mysql sometext....
:
AM_TIMETABLE
表
UserId Date_Time
101 2012-08-08 04:00:00
102 2012-08-15 10:00:00
103 2012-08-18 09:00:00
104 2012-08-24 05:00:00
我的问题是,我正在传递开始日期和结束日期以及CourseName
然后我希望以这种方式获取UserId我写了查询
select UserId,CourseName,course_desc from AM_COURSE where name='java' and UserId=(SELECT UserId FROM AM_TIMETABLE WHERE `Date_Time` BETWEEN '2012-08-07 00:00:00' AND '2012-08-20 00:00:00')
然后我将获得101 UserId值。
但我收到错误,因为内部查询给出两个id ... please help
答案 0 :(得分:1)
重命名每个表中的ID可能会有所帮助,可能是COURSE_ID和TIMETABLE_ID。在这种情况下,您还可以创建一个包含所有用户信息的表,例如姓名,电子邮件等。
一旦正确设置了表格,主键/外键配对将有助于连接表格并将数据输出为可想到的格式。
答案 1 :(得分:0)
你可以join
这两个表:
SELECT a.UserID
FROM AM_COURSE a
INNER JOIN AM_TIMETABLE b
ON a.UserID = b.USerID
WHERE a.CourseName = 'java' AND
b.Date_Time BETWEEN '2012-08-07 00:00:00' AND '2012-08-20 00:00:00'
答案 2 :(得分:0)
SELECT c.UserId
FROM AM_COURSE c
JOIN AM_TIMETABLE t
ON c.UserId = t.UserId
WHERE c.name = 'java'
AND t.Date_Time BETWEEN '2012-08-07 00:00:00' AND '2012-08-20 00:00:00'
答案 3 :(得分:0)
这个怎么样
select c.UserId,CourseName,course_desc from AM_COURSE c where name='java' and c.UserId=(SELECT t.UserId FROM AM_TIMETABLE t WHERE `Date_Time` BETWEEN '2012-08-07 00:00:00' AND '2012-08-20 00:00:00')
答案 4 :(得分:0)
如果您的内部查询返回的结果超过1(当然这对您的方案有效),那么您不应该使用 IN 子句吗?
select UserId
,CourseName
,course_desc
from AM_COURSE
where name='java' and UserId in (
SELECT UserId
FROM AM_TIMETABLE
WHERE `Date_Time` BETWEEN '2012-08-07 00:00:00' AND '2012-08-20 00:00:00'
)