Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql join语句中的括号应该放在哪里?_Sql_Join - Fatal编程技术网

Sql join语句中的括号应该放在哪里?

Sql join语句中的括号应该放在哪里?,sql,join,Sql,Join,执行此语句: select Item.ItemId, ProductGroup.ProductGroupId, ItemUom.ContentQty from Item join ItemUom on ItemUom.Item = Item.Oid where ItemUomId = 'Stuks' join ProductGroup on Item.ProductGroup = ProductGroup.Oid 给出此错误: “关键字“join”附近的语法

执行此语句:

select  Item.ItemId, 
        ProductGroup.ProductGroupId,
        ItemUom.ContentQty
from Item
join ItemUom on ItemUom.Item = Item.Oid where ItemUomId = 'Stuks'
join ProductGroup on Item.ProductGroup = ProductGroup.Oid
给出此错误:

“关键字“join”附近的语法不正确


当我反转这两条join语句时,它就起作用了。我想我应该在第一条join语句中的某个地方放上一些括号,但是在哪里?

where
应该放在
join
s之后:

select Item.ItemId, ProductGroup.ProductGroupId, ItemUom.ContentQty
from Item join 
     ItemUom 
     on ItemUom.Item = Item.Oid join 
     ProductGroup 
     on Item.ProductGroup = ProductGroup.Oid
where ItemUomId = 'Stuks';
您还可以添加
并使用
加入

on ItemUom.Item = Item.Oid and ItemUomId = 'Stuks'
但是,
where
after
join
s更具可读性


注意:使用
left
表中的
where
子句时要小心。使用
left join
时,它将强制外部连接到内部连接。

当您反转它们时,它会起作用,因为反转它们会将where放在正确的位置:

select  Item.ItemId, 
        ProductGroup.ProductGroupId,
        ItemUom.ContentQty
from Item
join ItemUom on ItemUom.Item = Item.Oid --where ItemUomId = 'Stuks'
join ProductGroup on Item.ProductGroup = ProductGroup.Oid

where ItemUomId = 'Stuks'

我们可以用两种方法解决这个问题
1.无where条款

select  Item.ItemId, 
                ProductGroup.ProductGroupId,
                ItemUom.ContentQty
        from Item
        join ItemUom on ItemUom.Item = Item.Oid and ItemUomId = 'Stuks' /*where ItemUomId = 'Stuks'*/
        join ProductGroup on Item.ProductGroup = ProductGroup.Oid
  • 使用where子句

  • Where子句应该添加到查询的末尾,不包括使用子查询时的情况。

    用您正在使用的数据库标记您的问题。
         select  Item.ItemId, 
                            ProductGroup.ProductGroupId,
                            ItemUom.ContentQty
                    from Item
                    join ItemUom on ItemUom.Item = Item.Oid
                    join ProductGroup on Item.ProductGroup = ProductGroup.Oid
                    where ItemUomId = 'Stuks'