【发布时间】:2019-10-19 22:59:58
【问题描述】:
我正在尝试让这个 MySQL 查询在 laravel 5.7 查询生成器中工作。 它在 phpmyadmin 中运行良好
SELECT c.Symbol
, s.SectorName
, cprs.strenght
, s.parentid
, ssbpi.Risklevel
, ssbpi.ColumnType
FROM Companies AS c
JOIN Sectors AS s ON s.SectorID = c.SectorID
JOIN Company_PriceRS AS cprs ON cprs.CompanyID = c.CompanyID
JOIN SubSectorsBPIsData AS ssbpi ON ssbpi.subcategoryid = s.parentid
WHERE cprs.PostDate = '2017-05-08'
AND WHERE CompanyPriceRS.strenght = 'strong'
AND WHERE SubSectorsBPIsData.ColumnType = $ColumnType
ColumnType 是一个来自下拉列表的变量,它已经被捕获并正常工作。
我已经根据文档尝试了正常的方式:
$Completequerytry1 = DB::table('Companies')
->join('Sectors', 'Sectors.SectorID', '=', 'Companies.SectorID')
->join('CompanyPriceRS', 'CompanyPriceRS.CompanyID', '=', 'Companies.CompanyID')
->$join('SubSectorsBPIsData ', 'SubSectorsBPIsData.subcategoryid', '=', 'Sectors.parentid')
->where('CompanyPriceRS.strenght', '=', 'strong')
->where('SubSectorsBPIsData.ColumnType', '=', $ColumnType)
->where('CompanyPriceRS.Postdate', '=', '2017-05-08');
->select('Companies.Symbol', 'Sectors.SectorName', 'CompanyPriceRS.strenght', 'Sectors.parentid', 'SubSectorsBPIsData.subcategoryid','SubSectorsBPIsData.ColumnType')
->limit(10);
->select('Companies.Symbol', 'Sectors.SectorName', 'CompanyPriceRS.strenght', 'Sectors.parentid', 'SubSectorsBPIsData.subcategoryid','SubSectorsBPIsData.ColumnType')
->limit(10);
echo '<pre>';
print_r($Completequerytry1);
错误: Symfony\Component\Debug\Exception\FatalThrowableError 抛出消息“语法错误,意外'->' (T_OBJECT_OPERATOR)”
- 使用具有多个嵌套连接的函数:
$Completequerytry1 = DB::table('Companies')
->join('Sectors', function ($join) use ($ColumnType) {
$join->on('Sectors.SectorID', '=', 'Companies.SectorID')
->join('CompanyPriceRS', function ($join2) {
$join2->on('CompanyPriceRS.CompanyID', '=', 'Companies.CompanyID')
->join('SubSectorsBPIsData', function ($join3) {
$join3->on('SubSectorsBPIsData.subcategoryid', '=', 'Sectors.parentid')
->where(function ($query1) {
$query1->where('CompanyPriceRS.strenght', '=', 'strong') //filter 1
->where('SubSectorsBPIsData.ColumnType', '=', $ColumnType) //filter2
->where('CompanyPriceRS.Postdate', '=', '2017-05-08'); // filter 3
});
});
});
})
->select('Companies.Symbol', 'Sectors.SectorName', 'CompanyPriceRS.strenght', 'Sectors.parentid', 'SubSectorsBPIsData.subcategoryid','SubSectorsBPIsData.ColumnType')
->limit(10);
echo '<pre>';
print_r($Completequerytry1);
错误: ErrorException (E_NOTICE) 未定义变量:ColumnType
3:然后尝试了嵌套 WHERE 的函数
$Completequerytry1 = DB::table('Companies')
->join('Sectors', 'Sectors.SectorID', '=', 'Companies.SectorID')
->join('CompanyPriceRS', 'CompanyPriceRS.CompanyID', '=', 'Companies.CompanyID')
->$join('SubSectorsBPIsData ', 'SubSectorsBPIsData.subcategoryid', '=', 'Sectors.parentid') //ERROR IS GIVEN ON THIS LINE
->where(function ($query1) {
$query1->where('CompanyPriceRS.strenght', '=', 'strong')
->where('SubSectorsBPIsData.ColumnType', '=', $ColumnType)
->where('CompanyPriceRS.Postdate', '=', '2017-05-08');
});
->select('Companies.Symbol', 'Sectors.SectorName', 'CompanyPriceRS.strenght', 'Sectors.parentid', 'SubSectorsBPIsData.subcategoryid','SubSectorsBPIsData.ColumnType')
->limit(10);
echo '<pre>';
print_r($Completequerytry1);
错误: 未定义变量:加入
仍然不知道我错过了什么。 我应该为 JOIN 和 WHERE 创建函数吗? 没有想法了。提前感谢您的见解:)
【问题讨论】:
-
PHP 中有语法错误。所以检查并修复你的代码。
-
请在代码问题中给出minimal reproducible example--剪切&粘贴&运行代码;具有期望和实际输出的示例输入(包括逐字错误消息);标签和明确的规范和解释。这包括您可以提供的最少代码,即您显示的代码可以通过您显示的代码扩展为不正常。 (调试基础。) PS您的问题是语法错误。您首先应该尽可能多地表明组成子表达式是正常的。明确说明您的问题是关于 那个错误 并在稍后的新帖子中询问您的总体目标。每个帖子问一个问题。
-
当你找不到语法错误时,把你的代码砍掉,直到它有效,然后再加回最后一个变得无效的东西。这里有很多语法错误。
标签: mysql laravel-5 join php-7 laravel-query-builder