[英]Adding fields to a complex SQL select statement
我需要從“ sys_acl_matrix”表中返回另外兩個字段的值,但是我不知道如何讀取這些AS和LEFT JOIN內容。
我需要添加的字段是:
AllowedCount
AllowedPeriodLen
這是查詢
$GLOBALS['MySQL']->getAllWithKey(
"SELECT `ta`.`ID`
AS `id`, `ta`.`Name`
AS `title`
FROM `sys_acl_actions`
AS `ta`
LEFT JOIN `sys_acl_matrix`
AS `tm`
ON `ta`.`ID`=`tm`.`IDAction`
LEFT JOIN `sys_acl_levels`
AS `tl`
ON `tm`.`IDLevel`=`tl`.`ID`
WHERE `tl`.`ID`='" . $iMembId . "'
ORDER BY `ta`.`Name`", "id");
如果有人可以幫助我了解此查詢的功能,那也很好。 關於左聯接我迷失了。
很難理解,因為格式不正確,但是:
$GLOBALS['MySQL']->getAllWithKey(
"SELECT `ta`.`ID` AS `id`,
`ta`.`Name` AS `title`,
`tm`.`AllowedCount`,
`tm`.`AllowedPeriodLen`
FROM `sys_acl_actions` AS `ta`
LEFT JOIN `sys_acl_matrix` AS `tm` ON `ta`.`ID` = `tm`.`IDAction`
LEFT JOIN `sys_acl_levels` AS `tl` ON `tm`.`IDLevel` = `tl`.`ID`
WHERE `tl`.`ID`='" . $iMembId . "'
ORDER BY `ta`.`Name`", "id");
我可能會取消所有的反引號,並確保該模式也不區分大小寫,但為了與問題保持一致,我保留了它們。
您還可以在WHERE子句中使用$iMembId
進行SQL注入-如果用戶在變量中提供了數據,則必須先清理數據,然后再將其添加到SQL中(記住Little Bobby Tables !)。 理想情況下,您將使用占位符(通常是問號)並在執行SQL時提供$iMembId
的值作為關聯值。
沒有這些反引號,它將變為:
$GLOBALS['MySQL']->getAllWithKey(
"SELECT ta.ID AS id,
ta.Name AS title,
tm.AllowedCount,
tm.AllowedPeriodLen
FROM sys_acl_actions AS ta
LEFT JOIN sys_acl_matrix AS tm ON ta.ID = tm.IDAction
LEFT JOIN sys_acl_levels AS tl ON tm.IDLevel = tl.ID
WHERE tl.ID = '" . $iMembId . "'
ORDER BY ta.Name", "id");
但是請注意,引號保留了內部標識符的大小寫,因此您可能必須在無引號版本生效之前修改架構。
左聯接通常返回左側表中的記錄,如果左側表有關聯的記錄則返回右側表中的記錄,但是左側的記錄仍然會返回。
您的查詢將導致sys_acl_matrix表中的記錄。我看不到您的查詢從其他2個表中返回任何其他列。您僅從sys_acl_actions表中返回2列。
更多信息: http : //www.tizag.com/mysqlTutorial/mysqlleftjoin.php
嘗試
$GLOBALS['MySQL']->getAllWithKey(
"SELECT `ta`.`ID`
AS `id`, `ta`.`Name`
AS `title`,
`tm`.`AllowedCount`, `tm`.`AllowedPeriodLen`
FROM `sys_acl_actions`
AS `ta`
LEFT JOIN `sys_acl_matrix`
AS `tm`
ON `ta`.`ID`=`tm`.`IDAction`
LEFT JOIN `sys_acl_levels`
AS `tl`
ON `tm`.`IDLevel`=`tl`.`ID`
WHERE `tl`.`ID`='" . $iMembId . "'
ORDER BY `ta`.`Name`", "id");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.