简体   繁体   English

MSSQL LEFT JOIN在同一服务器上的两个数据库之间,在使用PDO时不返回任何内容

[英]MSSQL LEFT JOIN between two databases on the same server, doesn't return anything when using PDO

I'm using PDO to connect to an MS SQL 2000 database and want to create a left join to another database on the same SQL 2000 server. 我正在使用PDO连接到MS SQL 2000数据库,并希望在同一SQL 2000服务器上创建另一个数据库的左连接。 This query returns the correct results when run on the MSSQL query analyser and pulls the data from both databases as I'm expecting. 当在MSSQL查询分析器上运行时,此查询返回正确的结果,并按照我的预期从两个数据库中提取数据。

select t_job.pk_Job, t_JobSummary.Description, t_CarChecks.YesNo
from t_job
left join t_JobSummary on pk_Job=t_JobSummary.fk_Job
left join DATABASE2.dbo.t_CarChecks t_CarChecks on pk_Job=t_CarChecks.fk_Job
where t_JobSummary.Description_vc LIKE \'Cars %\'

However, when I use the same query from the web-page, no results are returned. 但是,当我从网页使用相同的查询时,不会返回任何结果。 The query worked from the web-page until I added the LEFT JOIN to the second database and if I comment out that database as shown below, the query returns the results so I can be sure(?!) that the problem lies with the SQL statement. 查询在网页上工作,直到我将LEFT JOIN添加到第二个数据库,如果我注释掉该数据库,如下所示,查询返回结果,所以我可以肯定(?!)问题出在SQL上声明。

select t_job.pk_Job, t_JobSummary.Description --, t_CarChecks.YesNo
from t_job
left join t_JobSummary on pk_Job=t_JobSummary.fk_Job
--left join DATABASE2.dbo.t_CarChecks t_CarChecks on pk_Job=t_CarChecks.fk_Job
where t_JobSummary.Description_vc LIKE \'Cars %\'

After reading here and on other sites, I've used an alias for the second database but I still don't get any results. 在这里和其他网站上阅读后,我使用了第二个数据库的别名,但我仍然没有得到任何结果。

I'm new to PDO and this has me stumped. 我是新来的PDO,这有我难住了。 Any helpful pointers (or letting me know that there is an obvious mistake here), would be gratefully appreciated. 任何有用的指示(或让我知道这里有一个明显的错误),将不胜感激。

Thanks! 谢谢!

Thanks for the very quick responses, however, I've found the issue. 但是,感谢您的快速回复,我发现了这个问题。 Completely unrelated to PDO, it was the permissions on the second table in the database. 与PDO完全无关,它是数据库中第二个表的权限。 Testing from the SQL Server query analyser retuned the results because I had permissions on the new table; SQL Server查询分析器的测试重新调整了结果,因为我对新表有权限; the user I'm using to query the table with PDO did not... 我用来用PDO查询表的用户没有......

One for the 'Check this before posting on Stack-Overflow again' notebook... 一个用于'在再次发布Stack-Overflow之前检查此'笔记本...

The answer to the Ultimate Question Of Life The Universe And PDO issues is 生命终极问题的答案宇宙和PDO问题是

$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

set right after connect. 连接后立即设置。
it will retell you whatever error occurred in database 它将重新报告数据库中发生的任何错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM