Mysql 左连接案例时
Mysql Left Join Case When
我有以下 mysql table 个字段
|action_id|timestamp|user|module|action|object|info|interface|
内容如下:
69 |2021-12-06 22:15:43|1 |CATALOGUING|ADD|24 |item |intranet
122058|2022-02-23 09:47:17|13|CATALOGUING|ADD|10338|biblio|intranet
这是我正在处理的 mysql 查询:
SELECT action_id, i.barcode, CONCAT('<a href=\"/cgi-bin/koha/catalogue/detail.pl?biblionumber=',biblio.biblionumber,'\">',b.title,'</a>') AS Title, biblio.author, al.timestamp, CONCAT('<a href=\"/cgi-bin/koha/members/moremember.pl?borrowernumber=',borrowers.borrowernumber,'\">',borrowers.surname, ', ', borrowers.firstname,'</a>') AS Librarian, i.permanent_location, (CASE WHEN al.info LIKE "biblio%" THEN 'biblio' ELSE 'item' END) as typeofentry, al.action as type_of_action, al.info from action_logs AS al LEFT JOIN borrowers ON al.user =
CASE WHEN al.info LIKE "biblio%" THEN
borrowers.borrowernumber LEFT JOIN biblio on al.object=biblio.biblionumber
ELSE borrowers.borrowernumber LEFT JOIN items AS i on al.object=i.itemnumber LEFT JOIN biblio on i.biblionumber=biblio.biblionumber
END
我正在尝试对 LEFT JOIN 使用 CASE WHEN,这样如果“信息”列是 biblio,则查询应该与 biblio 左连接并忽略与项目 table 的 LEFT JOIN。如果“信息”列是项目,那么它应该与项目左连接,然后与书目左连接。使用 LEFT JOIN 甚至可以做到这一点吗?这就是我一直试图效仿的:How to use a case statement to determine which field to left join on
您不能像这里那样进行 case/when 加入。由于您有不同的方式来处理 HTML link 引用,并且每个引用都有自己的书目记录上下文,因此您最好从两个不同的书目查询和 [=14 查询中获取片段=] 和联合他们。获取该结果并将其应用于您的 concat 过程。像
SELECT
PQ.action_id,
PQ.barcode,
CONCAT( '<a href=\"/cgi-bin/koha/catalogue/detail.pl?biblionumber=',
PQ.biblionumber, '\">', b.title, '</a>') Title,
PQ.author,
PQ.timestamp,
CONCAT( '<a href=\"/cgi-bin/koha/members/moremember.pl?borrowernumber=',
b.borrowernumber, '\">', b.surname, ', ',
b.firstname, '</a>') Librarian,
PQ.permanent_location,
PQ.typeofentry,
PQ.action as type_of_action,
PQ.info
from
(
SELECT
action_id,
i.barcode,
biblio.biblionumber,
biblio.author,
al.timestamp,
i.permanent_location,
'biblio' typeofentry,
al.action,
al.info,
al.user
from
action_logs al
LEFT JOIN items i
on al.object = i.itemnumber
LEFT JOIN biblio
on al.object = biblio.biblionumber
where
al.info like 'biblio%'
UNION ALL
SELECT
action_id,
i.barcode,
biblio.biblionumber,
biblio.author,
al.timestamp,
i.permanent_location,
'item' typeofentry,
al.action,
al.info,
al.user
from
action_logs al
LEFT JOIN items i
on al.object = i.itemnumber
LEFT JOIN biblio
on i.biblionumber = biblio.biblionumber
where
NOT al.info like 'biblio%'
) PQ
LEFT JOIN borrowers b
ON PQ.user = b.borrowernumber
我有以下 mysql table 个字段
|action_id|timestamp|user|module|action|object|info|interface|
内容如下:
69 |2021-12-06 22:15:43|1 |CATALOGUING|ADD|24 |item |intranet
122058|2022-02-23 09:47:17|13|CATALOGUING|ADD|10338|biblio|intranet
这是我正在处理的 mysql 查询:
SELECT action_id, i.barcode, CONCAT('<a href=\"/cgi-bin/koha/catalogue/detail.pl?biblionumber=',biblio.biblionumber,'\">',b.title,'</a>') AS Title, biblio.author, al.timestamp, CONCAT('<a href=\"/cgi-bin/koha/members/moremember.pl?borrowernumber=',borrowers.borrowernumber,'\">',borrowers.surname, ', ', borrowers.firstname,'</a>') AS Librarian, i.permanent_location, (CASE WHEN al.info LIKE "biblio%" THEN 'biblio' ELSE 'item' END) as typeofentry, al.action as type_of_action, al.info from action_logs AS al LEFT JOIN borrowers ON al.user =
CASE WHEN al.info LIKE "biblio%" THEN
borrowers.borrowernumber LEFT JOIN biblio on al.object=biblio.biblionumber
ELSE borrowers.borrowernumber LEFT JOIN items AS i on al.object=i.itemnumber LEFT JOIN biblio on i.biblionumber=biblio.biblionumber
END
我正在尝试对 LEFT JOIN 使用 CASE WHEN,这样如果“信息”列是 biblio,则查询应该与 biblio 左连接并忽略与项目 table 的 LEFT JOIN。如果“信息”列是项目,那么它应该与项目左连接,然后与书目左连接。使用 LEFT JOIN 甚至可以做到这一点吗?这就是我一直试图效仿的:How to use a case statement to determine which field to left join on
您不能像这里那样进行 case/when 加入。由于您有不同的方式来处理 HTML link 引用,并且每个引用都有自己的书目记录上下文,因此您最好从两个不同的书目查询和 [=14 查询中获取片段=] 和联合他们。获取该结果并将其应用于您的 concat 过程。像
SELECT
PQ.action_id,
PQ.barcode,
CONCAT( '<a href=\"/cgi-bin/koha/catalogue/detail.pl?biblionumber=',
PQ.biblionumber, '\">', b.title, '</a>') Title,
PQ.author,
PQ.timestamp,
CONCAT( '<a href=\"/cgi-bin/koha/members/moremember.pl?borrowernumber=',
b.borrowernumber, '\">', b.surname, ', ',
b.firstname, '</a>') Librarian,
PQ.permanent_location,
PQ.typeofentry,
PQ.action as type_of_action,
PQ.info
from
(
SELECT
action_id,
i.barcode,
biblio.biblionumber,
biblio.author,
al.timestamp,
i.permanent_location,
'biblio' typeofentry,
al.action,
al.info,
al.user
from
action_logs al
LEFT JOIN items i
on al.object = i.itemnumber
LEFT JOIN biblio
on al.object = biblio.biblionumber
where
al.info like 'biblio%'
UNION ALL
SELECT
action_id,
i.barcode,
biblio.biblionumber,
biblio.author,
al.timestamp,
i.permanent_location,
'item' typeofentry,
al.action,
al.info,
al.user
from
action_logs al
LEFT JOIN items i
on al.object = i.itemnumber
LEFT JOIN biblio
on i.biblionumber = biblio.biblionumber
where
NOT al.info like 'biblio%'
) PQ
LEFT JOIN borrowers b
ON PQ.user = b.borrowernumber