我已经建立了这样的查询:
SELECT
c.*,
(SELECT COUNT(cursus_id) FROM cursusdienst_bestellingen where cursus_id = c.id AND datum_afhaling IS NULL AND datum_verwijderd IS NULL AND studentid = '$studentid') as besteld,
(SELECT COUNT(cursus_id) FROM cursusdienst_bestellingen where cursus_id = c.id AND datum_afhaling IS NOT NULL AND datum_verwijderd IS NULL AND datum_afgehaald IS NULL AND studentid = '$studentid') as afhalen,
(SELECT datum_afhaling + INTERVAL 14 DAY FROM cursusdienst_bestellingen where cursus_id = c.id AND datum_afhaling IS NOT NULL AND datum_verwijderd IS NULL AND datum_afgehaald IS NULL AND studentid = '$studentid') as datum_afhaling,
(SELECT COUNT(cursus_id) FROM cursusdienst_bestellingen where cursus_id = c.id AND datum_afgehaald IS NOT NULL AND datum_verwijderd IS NULL AND cb.studentid = '$studentid') as afgehaald
FROM
cursusdienst c
LEFT JOIN
cursusdienst_bestellingen cb
ON
cb.cursus_id = c.id
WHERE
c.studierichting = '1ste Bachelor'
ORDER BY
c.artikel asc, cb.studentid
这是学生和课程的注册/订购系统。有很多学生使用这个系统。学生必须先注册才能订购课程。 我的查询存在问题,因为我看到同一个列表中每个学生的课程。这里有一个例子:
Checkbox Course Price Status
Checkbox Course 1 12.00 Ordered
Checkbox Course 1 12.00 Ordered
Checkbox Course 2 25.50 Ready to deliver
Checkbox Course 3 15.00
如您所见,查询向我显示了2名学生的所有课程信息(仅作为示例)他们已登录并订购了课程。 第一个学生订购了Course 1 en 2,第二个学生只订购了Course 1。 如果我以学生身份登录,我只想看到我订购的课程的状态,但我也看到其他课程(没有任何状态,如课程3),所以我可以订购相同形式的其他课程。 不想看到的是其他人的订单!
所以我想只看到这个,如果我作为一名学生,订购了课程1和2.课程3也在列表中,因为我现在可以订购,但我之前没有订购(所以它没有状态):
Checkbox Course Price Status
Checkbox Course 1 12.00 Ordered
Checkbox Course 2 25.50 Ready to deliver
Checkbox Course 3 15.00
如果我添加"并且cb.studentid =' $ studentid'"在那里我只看到我订购的课程,而不是我没有订购的其他课程。
编辑:(感谢SunKnight0)设置" AND cb.studentid =' $ studentid'"在ON部分而不是在WHERE部分!因此,工作代码如下所示:
SELECT
c.*,
(SELECT COUNT(cursus_id) FROM cursusdienst_bestellingen where cursus_id = c.id AND datum_afhaling IS NULL AND datum_verwijderd IS NULL AND studentid = '$studentid') as besteld,
(SELECT COUNT(cursus_id) FROM cursusdienst_bestellingen where cursus_id = c.id AND datum_afhaling IS NOT NULL AND datum_verwijderd IS NULL AND datum_afgehaald IS NULL AND studentid = '$studentid') as afhalen,
(SELECT datum_afhaling + INTERVAL 14 DAY FROM cursusdienst_bestellingen where cursus_id = c.id AND datum_afhaling IS NOT NULL AND datum_verwijderd IS NULL AND datum_afgehaald IS NULL AND studentid = '$studentid') as datum_afhaling,
(SELECT COUNT(cursus_id) FROM cursusdienst_bestellingen where cursus_id = c.id AND datum_afgehaald IS NOT NULL AND datum_verwijderd IS NULL AND cb.studentid = '$studentid') as afgehaald
FROM
cursusdienst c
LEFT JOIN
cursusdienst_bestellingen cb
ON
cb.cursus_id = c.id AND cb.student_id = '$studentid'
WHERE
c.studierichting = '1ste Bachelor'
ORDER BY
c.artikel asc, cb.studentid
答案 0 :(得分:0)
SELECT
c.*,
(SELECT COUNT(cursus_id) FROM cursusdienst_bestellingen where cursus_id = c.id AND datum_afhaling IS NULL AND datum_verwijderd IS NULL AND studentid = '$studentid') as besteld,
(SELECT COUNT(cursus_id) FROM cursusdienst_bestellingen where cursus_id = c.id AND datum_afhaling IS NOT NULL AND datum_verwijderd IS NULL AND datum_afgehaald IS NULL AND studentid = '$studentid') as afhalen,
(SELECT datum_afhaling + INTERVAL 14 DAY FROM cursusdienst_bestellingen where cursus_id = c.id AND datum_afhaling IS NOT NULL AND datum_verwijderd IS NULL AND datum_afgehaald IS NULL AND studentid = '$studentid') as datum_afhaling,
(SELECT COUNT(cursus_id) FROM cursusdienst_bestellingen where cursus_id = c.id AND datum_afgehaald IS NOT NULL AND datum_verwijderd IS NULL AND cb.studentid = '$studentid') as afgehaald
FROM
cursusdienst c
LEFT JOIN
cursusdienst_bestellingen cb
ON
cb.cursus_id = c.id AND cb.studentid = '$studentid'
WHERE
c.studierichting = '1ste Bachelor'
ORDER BY
c.artikel asc, cb.studentid