其中的语法不正确

时间:2017-09-20 12:18:55

标签: sql

以下是我正在尝试编写的查询。但我收到一个错误,说错误的语法附近

SELECT
    [p.ProjectId],
    SUM(pf.size),
    COUNT(pr.ServerGroupId) AS [UnRestrictedFacility],
    CASE   
       WHEN COUNT(ps.CreatedBy) > 0 
          THEN 1 
          ELSE 0 
    END 
FROM
    Project p 
INNER JOIN
    ProjectFile pf 
INNER JOIN
    ProjectPartlink ppl ON pf.ProjectPartlinkId = ppl.ProjectPartlinkid  
                        AND ppl.ProjectId = p.projectid 
INNER JOIN
    ProjectRestriction pr ON pr.projectid = p.projectid 
INNER JOIN
    ProjectPreseed ps ON ps.projectid = p.projectid 
WHERE
    p.projectid = @projectid

有人可以帮忙吗?

5 个答案:

答案 0 :(得分:2)

这是错误:

<table>
<tr ng-repeat="parent in list">
    <td ng-repeat="child in parent">
        <table>
            <tr ng-repeat="grandChild in child">
              <td ng-repeat="item in grandChild">
                {{item}}
              <td>
             </tr>
        </table>
    </td>
</tr></table>

应该是

<table>
<tr ng-repeat="parent in list">
    <td ng-repeat="child in parent">
        <div ng-repeat="grandChild in child">
            <div ng-repeat="item in grandChild">
                {{item}}
            </div>
        </div>          
    </td>
</tr></table>

答案 1 :(得分:1)

错误有点误导:

function simpleNewsAlter_simplenews_subscriptions_block_ico_subscription_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  $form['system_messages'] = [
    '#markup' => '<div id="' . Html::getClass($form_id) . '-messages"></div>',
    '#weight' => -100,
  ];
  $form['actions']['subscribe']['#ajax']  = [
    'callback' => '\Drupal\simplenews\Form\SubscriptionsBlockForm::submitSubscribe',
    'event' => 'click',
    'progress' => [
      'type' => 'throbber',
    ],
  ];
}

您错过了第一次加入的namespace MainAppHotelXamarin { public class AdminActivity : Activity { string dpPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "HotelDb.db3"); Button BtnOrderFood; protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.Admin); BtnOrderFood.Click += BtnOrderFood_Click; CreateDB(); } private void BtnOrderFood_Click(object sender, EventArgs e) { StartActivity(typeof(FoodOrdersListActivity)); } public string CreateDB() { var db = new SQLiteConnection(dpPath); } } }

修改

您还使用没有From Project p inner join ProjectFile pf inner join ProjectPartlink ppl 的聚合函数。据我所知,这只是旧版本的mysql所允许的。如果使用聚合,则必须使用ON子句来获得可靠的结果。你没有提到你使用的rdbms,但这也可能是导致错误的原因。

答案 2 :(得分:1)

首先,从长远来看,清理并拥有格式良好的SQL将对您有所帮助。始终将您的“ON”条款与相应的联接放在一起,这样您就知道自己没有忘记某些事情并且在路上有笛卡尔结果。另外,我个人的偏好是始终使用leftSideAlias.column = rightSideAlias.column列出ON子句。它还有助于显示表A-&gt; B-&gt; C等之间的关系。你实际上DID拥有查询的所有部分,我只是重新组织它们。注意从一个表到下一个表的可读性。

SELECT
      p.ProjectId,
      sum(pf.size),
      count(pr.ServerGroupId) UnRestrictedFacility,
      CASE when count(ps.CreatedBy) > 0 then 1 Else 0 END CreatedCounter
   From 
      Project p 
         inner join ProjectPartlink ppl  
            on p.projectid = ppl.ProjectId
            inner join ProjectFile pf 
               on ppl.ProjectPartlinkid = pf.ProjectPartlinkId
         inner join ProjectRestriction pr
            on p.projectid = pr.projectid
         inner join ProjectPreseed ps   
            on p.projectid = ps.projectid 
   where 
      p.projectid = @parmProjectid

最后,你的WHERE子句。您有@ProjectID,我知道在提供变量时会引起混淆。如果该名称没有变量,但是该名称有一列,则它似乎会解析为列名。为了防止这种情况,我喜欢明确地处理某种类型的前缀,例如从@ProjectID到@parmProjectID,所以我知道它来自某个参数而不是错误的列名引用。

答案 3 :(得分:0)

由于默认的操作顺序

,这也应该有效
   From Project p 
   inner join ProjectFile pf 
   inner join ProjectPartlink ppl 
   on pf.ProjectPartlinkId=ppl.ProjectPartlinkid  
   on ppl.ProjectId=p.projectid 

答案 4 :(得分:0)

你应该尝试:

$data = DB::select($your_select_query);