我需要检查日志表中的数据,看看用户之前是否读过帖子。我的日志表名为 foretag_kontaktervisadbrf2017 ,其中包含有关谁阅读了帖子的信息。如何编辑我的查询以便获取信息,告诉我我是否阅读过帖子?
这是我目前的查询:
SELECT
brfbolagsverket.BrfNamn
, brfextra.BrfId
, brfextra.Organisationsnr
, brfextra.Gatuadress
, brfextra.Ort
, brfextra.Lagenheter
, brfextra.ByggAr
, BrfHarFastighet.Fangdatum
FROM
(((((brfextra LEFT JOIN BrfHarFastighet ON brfextra.Organisationsnr = BrfHarFastighet.Organisationsnr)
LEFT JOIN brfkonkurs ON brfextra.Organisationsnr = brfkonkurs.Organisationsnr)
LEFT JOIN brfavford ON brfextra.Organisationsnr = brfavford.Organisationsnr)
LEFT JOIN brffusion ON brfextra.Organisationsnr = brffusion.Organisationsnr)
LEFT JOIN brflikvidation ON brfextra.Organisationsnr = brflikvidation.Organisationsnr)
LEFT JOIN brfbolagsverket ON brfextra.Organisationsnr = brfbolagsverket.Organisationsnr
WHERE
(((brfkonkurs.BolagetsStatusKod) IS NULL)
AND ((brfavford.BolagetsStatusKod) IS NULL)
AND ((brffusion.BolagetsStatusKod) IS NULL)
AND ((brflikvidation.BolagetsStatusKod) IS NULL OR (brflikvidation.BolagetsStatusKod) = 34))
在结果中,我需要日志表中的以下附加列:
BrfId, BrfNamn, Organisationsnr, LogEntry
1, Billy, Organisationsnr, You have shown this post before
2, Carl, Organisationsnr, NULL
3, Kent, Organisationsnr, NULL
4, Sara, Organisationsnr, You have shown this post before
请帮助我需要添加到查询中的代码。我缺乏编码经验。
答案 0 :(得分:1)
我会通过添加如下例子的子选择来做到这一点。希望它能帮助您指明正确的方向。
SELECT
brfbolagsverket.BrfNamn
, CASE WHEN (SELECT COUNT(*) FROM foretag_kontaktervisadbrf2017 AS L WHERE L.Organisationsnr = B.Organisationsnr AND L.Foretag_kontaktID = 1247) > 0 THEN 'Lest' ELSE 'Inte lest' END AS NotificationRead
, brfextra.BrfId
, brfextra.Organisationsnr
, brfextra.Gatuadress
, brfextra.Ort
, brfextra.Lagenheter
, brfextra.ByggAr
, BrfHarFastighet.Fangdatum
FROM (((((
brfextra AS B
LEFT JOIN BrfHarFastighet ON brfextra.Organisationsnr = BrfHarFastighet.Organisationsnr)
LEFT JOIN brfkonkurs ON brfextra.Organisationsnr = brfkonkurs.Organisationsnr)
LEFT JOIN brfavford ON brfextra.Organisationsnr = brfavford.Organisationsnr)
LEFT JOIN brffusion ON brfextra.Organisationsnr = brffusion.Organisationsnr)
LEFT JOIN brflikvidation ON brfextra.Organisationsnr = brflikvidation.Organisationsnr)
LEFT JOIN brfbolagsverket ON brfextra.Organisationsnr = brfbolagsverket.Organisationsnr
WHERE (((brfkonkurs.BolagetsStatusKod) Is Null) AND ((brfavford.BolagetsStatusKod) Is Null) AND ((brffusion.BolagetsStatusKod) Is Null) AND ((brflikvidation.BolagetsStatusKod) Is Null Or (brflikvidation.BolagetsStatusKod)=34))
答案 1 :(得分:0)
对不起,如果我发错了 - 不知道如何正确回答Werner Waage的答案。
当搜索获得超过50-70条记录时,查询速度很慢 - 我尝试更改为选择
(SELECT Foretag_kontaktervisadBrfID FROM foretag_kontaktervisadbrf2017 AS L WHERE L.Organisationsnr = B.Organisationsnr AND L.Foretag_kontaktID = '1247') AS NotificationRead
但它的速度很慢。
我的日志表 foretag_kontaktervisadbrf2017 有这样的独特帖子
Foretag_kontaktervisadBrfID, Foretag_kontaktID, Organisationsnr, Date001
1, Billy01, Acme_01, 2018-01-01
2, Carl01, Acme_02, 2018-01-02
3, Billy01, Acme_02, 2018-04-15
4, Carl01, Acme_03, 2018-14-14
有什么方法可以将我以前的速度恢复到新查询? 我的原始SQL查询工作速度很快,即使有1000条记录/帖子的结果。
答案 2 :(得分:0)
这是另一种不使用子选择的方法,它假设表 foretag_kontaktervisadbrf2017 每个用户包含0或1行。
SELECT
brfbolagsverket.BrfNamn
, CASE WHEN L.Foretag_kontaktID IS NOT NULL THEN 'Lest' ELSE 'Inte lest' END AS NotificationRead
, brfextra.BrfId
, brfextra.Organisationsnr
, brfextra.Gatuadress
, brfextra.Ort
, brfextra.Lagenheter
, brfextra.ByggAr
, BrfHarFastighet.Fangdatum
FROM (((((
brfextra AS B
LEFT JOIN BrfHarFastighet ON brfextra.Organisationsnr = BrfHarFastighet.Organisationsnr)
LEFT JOIN brfkonkurs ON brfextra.Organisationsnr = brfkonkurs.Organisationsnr)
LEFT JOIN brfavford ON brfextra.Organisationsnr = brfavford.Organisationsnr)
LEFT JOIN brffusion ON brfextra.Organisationsnr = brffusion.Organisationsnr)
LEFT JOIN brflikvidation ON brfextra.Organisationsnr = brflikvidation.Organisationsnr)
LEFT JOIN brfbolagsverket ON brfextra.Organisationsnr = brfbolagsverket.Organisationsnr
LEFT JOIN foretag_kontaktervisadbrf2017 AS L ON L.Organisationsnr = B.Organisationsnr AND L.Foretag_kontaktID = 1247 -- This id probably needs to come from somewhere else, join in the users etc..
WHERE (((brfkonkurs.BolagetsStatusKod) Is Null) AND ((brfavford.BolagetsStatusKod) Is Null) AND ((brffusion.BolagetsStatusKod) Is Null) AND ((brflikvidation.BolagetsStatusKod) Is Null Or (brflikvidation.BolagetsStatusKod)=34))