如何将原始 SQL 查询转换为 Silverstripe SQL 查询抽象层
How to convert raw SQL query into Silverstripe SQLQuery abstraction layer
我有一个页面,我正在尝试从数据库中提取与该页面相关的文章。我有 SQL 查询可以提取我需要的内容,但我一直收到错误 "Unknown column 'Fashion' in 'where clause'"。我相信我需要将它从
$FilteredStories = DB::query(' SELECT C.ID, C.URLSegment, C.Title, B.Title AS "Category"
FROM `articlepage_categories` AS A
JOIN articlecategory AS B ON A.ArticleCategoryID = B.ID
JOIN sitetree AS C ON A.ArticlePageID = C.ID
WHERE B.Title = "Fashion" LIMIT 5')
->value();
进入SQL查询抽象层,但我不知道如何。有人可以告诉我如何创建具有多个连接的 SQL 查询抽象层吗?
备注
- 我正在使用 Silverstripe 版本 3.6.1
- "Fashion" 目前是硬编码的,但将被替换为
我将传入的变量。
SilverStripe 的数据库默认使用ANSI
sql_mode
,其中字符串文字需要用单引号括起来。您需要将 "Fashion"
周围的双引号替换为单引号,如下所示:
$FilteredStories = DB::query('SELECT C.ID, C.URLSegment, C.Title, B.Title AS "Category"
FROM `articlepage_categories` AS A
JOIN articlecategory AS B ON A.ArticleCategoryID = B.ID
JOIN sitetree AS C ON A.ArticlePageID = C.ID
WHERE B.Title = \'Fashion\' LIMIT 5')
此处转义,因为外引号是单引号。
您的查询将用 SQLSelect
表示,如下所示:
$filteredStories = SQLSelect::create();
$filteredStories->selectField('"sitetree"."ID", "sitetree"."URLSegment", "sitetree"."Title", "articlecategory"."Title" AS "Category"');
$filteredStories->setFrom('articlepage_categories');
$filteredStories->addLeftJoin('articlecategory', '"articlecategory"."ID" = "articlepage_categories"."ArticleCategoryID"');
$filteredStories->addLeftJoin('sitetree','"sitetree"."ID" = "articlepage_categories"."ArticlePageID"');
$filteredStories->addWhere('"articlecategory"."Title" = \'Fashion\'');
$filteredStories->setLimit(5);
我有一个页面,我正在尝试从数据库中提取与该页面相关的文章。我有 SQL 查询可以提取我需要的内容,但我一直收到错误 "Unknown column 'Fashion' in 'where clause'"。我相信我需要将它从
$FilteredStories = DB::query(' SELECT C.ID, C.URLSegment, C.Title, B.Title AS "Category"
FROM `articlepage_categories` AS A
JOIN articlecategory AS B ON A.ArticleCategoryID = B.ID
JOIN sitetree AS C ON A.ArticlePageID = C.ID
WHERE B.Title = "Fashion" LIMIT 5')
->value();
进入SQL查询抽象层,但我不知道如何。有人可以告诉我如何创建具有多个连接的 SQL 查询抽象层吗?
备注
- 我正在使用 Silverstripe 版本 3.6.1
- "Fashion" 目前是硬编码的,但将被替换为 我将传入的变量。
SilverStripe 的数据库默认使用ANSI
sql_mode
,其中字符串文字需要用单引号括起来。您需要将 "Fashion"
周围的双引号替换为单引号,如下所示:
$FilteredStories = DB::query('SELECT C.ID, C.URLSegment, C.Title, B.Title AS "Category"
FROM `articlepage_categories` AS A
JOIN articlecategory AS B ON A.ArticleCategoryID = B.ID
JOIN sitetree AS C ON A.ArticlePageID = C.ID
WHERE B.Title = \'Fashion\' LIMIT 5')
此处转义,因为外引号是单引号。
您的查询将用 SQLSelect
表示,如下所示:
$filteredStories = SQLSelect::create();
$filteredStories->selectField('"sitetree"."ID", "sitetree"."URLSegment", "sitetree"."Title", "articlecategory"."Title" AS "Category"');
$filteredStories->setFrom('articlepage_categories');
$filteredStories->addLeftJoin('articlecategory', '"articlecategory"."ID" = "articlepage_categories"."ArticleCategoryID"');
$filteredStories->addLeftJoin('sitetree','"sitetree"."ID" = "articlepage_categories"."ArticlePageID"');
$filteredStories->addWhere('"articlecategory"."Title" = \'Fashion\'');
$filteredStories->setLimit(5);