出于某些原因,我必须从两个相关表中收集数据:
SELECT
Company.id AS CompanyID,
Company.name AS CompanyName,
-- Get New Project ID
convert(numeric,(CASE
WHEN CHARINDEX('"new_project";s:1:"1"',CompanyInfo.data) > 0
THEN (SELECT TOP(1) Project.id FROM Project
WHERE Project.Company_id=Company.id
AND (CAST(Project.CreateDate AS DATE)=CAST(CompanyInfo.date AS DATE)))
ELSE NULL END
)) as NewProjectID,
-- Get New Project Name:
(CASE
WHEN CHARINDEX('"new_project";s:1:"1"',CompanyInfo.data) > 0
THEN (SELECT TOP(1) Project.name FROM Project
WHERE Project.Company_id=Company.id
AND (CAST(Project.CreateDate AS DATE)=CAST(CompanyInfo.date AS DATE)))
ELSE NULL END
) as NewProjectName,
-- Get New Project Owner:
(CASE
WHEN CHARINDEX('"new_project";s:1:"1"',CompanyInfo.data) > 0
THEN (SELECT TOP(1) Project.owner FROM Project
WHERE Project.Company_id=Company.id
AND (CAST(Project.CreateDate AS DATE)=CAST(CompanyInfo.date AS DATE)))
ELSE NULL END
) as NewProjectOwner,
..........................
-- Gets are go on like those above...
-- Because MSSQL never merges sub-select results with main select result...
..........................
FROM
CompanyInfo
LEFT JOIN
Company ON Company.id=CompanyInfo.Company_id
获取正如上面那样继续,因为MSSQL永远不会将子选择结果与主选择结果合并...
SQL Server不允许我使用“NewProjectID”别名,就像选择块中的变量一样:
SELECT
-- Get New Project ID once:
convert(numeric,(CASE
WHEN CHARINDEX('"new_project";s:1:"1"',CompanyInfo.data) > 0
THEN (SELECT TOP(1) Project.id FROM Project
WHERE Project.Company_id=Company.id
AND (CAST(Project.CreateDate AS DATE)=CAST(CompanyInfo.date AS DATE)))
ELSE NULL END
)) as NewProjectID,
-- and use it for other subqueries:
(SELECT Project.name FROM Project WHERE Project.id=NewProjectID) as ProjectName,
(SELECT Project.owner FROM Project WHERE Project.id=NewProjectID) as ProjectOwner,
(SELECT Project.addy FROM Project WHERE Project.id=NewProjectID) as ProjectAddy,
...........
我尝试过很多东西:
DECLARE @ProjectID int ;
SELECT
-- Get New Project ID once:
(@ProjectID = convert(numeric,(CASE
.............
))) as NewProjectID,
.............
但是我遇到了错误,我错了,我只看到错误......
如何解决?
* PS: CompanyInfo.data列包含PHP样式的序列化数据,我无权重新设计表结构......