有没有办法可以改善这种SQL查询性能:
INSERT
INTO ...
WHERE NOT EXISTS(Validation...)
Run Code Online (Sandbox Code Playgroud)
问题是当我的表中有很多数据(如数百万行)时,WHERE NOT EXISTS如果非常慢,则执行该子句.我必须进行此验证,因为我无法插入重复数据.
我使用SQLServer 2005
谢谢
我刚刚开始自学Haskell.这段代码应该进行素数分解:
divides :: Integer -> Integer -> Bool
divides small big = (big `mod` small == 0)
lowestDivisor :: Integer -> Integer
lowestDivisor n = lowestDivisorHelper 2 n
where lowestDivisorHelper m n
| (m `divides` n) = m -- these should belong to lowestDivisorHelper
| otherwise = lowestDivisorHelper (m+1) n
primeFactors :: Integer -> [Integer]
primeFactors 1 = []
primeFactors n
| n < 1 = error "Must be positive"
| otherwise = let m = lowestDivisor n
in m:primeFactors …Run Code Online (Sandbox Code Playgroud) 我试图在一个函数中写3-4 where语句但我得到错误并且无法做到,我试图做类似的事情:
foo x=
| x == foo1 = 5
| x == foo2 =3
| x == foo3 =1
| otherwise =2
where foo1= samplefunct1 x
foo2= samplefunct2 x
foo3= samplefunct3 x
Run Code Online (Sandbox Code Playgroud)
我知道代码有点无用,但我只是写了这个来举例说明我的意思.
有没有人可以帮助我?提前致谢.
syntax haskell where-clause guard-clause function-definition
在阅读QuickCheck手册时,我遇到了以下示例:
prop_RevRev xs = reverse (reverse xs) == xs
where types = xs::[Int]
Run Code Online (Sandbox Code Playgroud)
手册继续说:
属性必须具有单形类型."多态"属性(例如上面的属性)必须限制为用于测试的特定类型.通过在a中声明一个或多个参数的类型来方便
其中types =(x1 :: t1,x2 :: t2,...)
条款.请注意,类型不是关键字; 这只是一个本地声明,它提供了一个方便的地方来限制x1,x2等的类型.
我之前从未在Haskell中看过这样的技巧.这是我真正遇到的问题:
为什么类型声明的这种语法甚至存在?以下哪些不能对我做什么?
prop_RevRev :: [Int] -> Bool
prop_RevRev xs = reverse (reverse xs) == xs
Run Code Online (Sandbox Code Playgroud)这种用法是否where构成了类型声明的"特殊"语法?或者它是否一致和合乎逻辑(如果是这样,如何?)?
这是使用标准还是传统的Haskell?
我知道这听起来是一个愚蠢的问题,但我想知道是否update可以在没有where条款的情况下使用查询.如果是这样的话.
提前致谢.
我需要在sql中的where子句中使用if语句.
Select * from Customer
WHERE (I.IsClose=@ISClose OR @ISClose is NULL)
AND
(C.FirstName like '%'+@ClientName+'%' or @ClientName is NULL )
AND
if (@Value=2)
begin
(I.RecurringCharge=@Total or @Total is NULL )
end
else if(@Value=3)
begin
(I.RecurringCharge like '%'+cast(@Total as varchar(50))+'%' or @Total is NULL )
end
Run Code Online (Sandbox Code Playgroud)
注意:这不是完整的代码.所有内容都在SP中定义.我只需编写理解该问题所需的代码.
提前致谢.
我正在尝试在where子句中使用计算列.
我已尝试从CROSS APPLY到子查询选择的所有内容,但它并没有给我任何接近我需要的东西.
我的查询到目前为止:
SELECT p.Code, c.AccountNumber, Sales = (SUM(p.UnitPrice) * SUM(od.QtyShipped)) FROM [dbo].Customer c
LEFT JOIN [dbo].OrderHeader oh ON oh.CustomerId = c.Id
LEFT JOIN [dbo].OrderDetail od ON od.OrderHeaderId = oh.Id
LEFT JOIN [dbo].Product p ON p.Id = od.ProductId
WHERE Sales > 100
GROUP BY p.Code, c.AccountNumber, Sales
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为'Sales'是无效列
没错,第十个条件列问题:
我正在编写一个存储过程,它接受一个映射到几个标志列之一的输入参数.筛选请求列的最佳方法是什么?我目前正在使用SQL2000,但即将转向SQL2008,所以如果可以的话,我会采用现代解决方案.
在sproc中查询的表格看起来像
ID ... fooFlag barFlag bazFlag quuxFlag
-- ------- ------- ------- --------
01 1 0 0 1
02 0 1 0 0
03 0 0 1 1
04 1 0 0 0
Run Code Online (Sandbox Code Playgroud)
我想做点什么
select ID, name, description, ...
from myTable
where (colname like @flag + 'Flag') = 1
Run Code Online (Sandbox Code Playgroud)
所以,如果我打电话给exec uspMyProc @flag = 'foo'我,我会回到第1和第4行.
我知道我不能直接在SQL中用parens做这个部分.为了做动态SQL,我必须将整个查询填充到一个字符串中,在WHERE子句中连接@flag param,然后执行字符串.除了在进行动态SQL时我得到的肮脏感觉,我的查询相当大(我选择了几十个字段,加入了5个表,调用了几个函数),所以它是一个巨大的字符串,因为一行在3行WHERE过滤器中.
或者,我可以有4个查询副本,并在CASE语句中选择它们.这使得SQL代码可以直接执行(并且受语法高亮等影响)但是以重复大块代码为代价,因为我不能仅在WHERE子句上使用CASE.
还有其他选择吗?可以应用任何棘手的连接或逻辑操作?或者我应该克服它并执行动态SQL?
我试图用case来改变我在where子句中检查的值,但是我得到了错误:
关键字'CASE'附近的语法不正确
SQL Server 2005
select *
from table
where ((CASE when adsl_order_id like '95037%'
then select '000000'+substring(adsl_order_id,6,6)
ELSE select adsl_order_id
END)
not in (select mwebID from tmp_csv_dawis_bruger0105)
Run Code Online (Sandbox Code Playgroud) 我在使用Doctrine制作一个相当简单的查询时遇到了麻烦......
我有两个数组($ countries,$ cities),我需要检查数据库记录值是否与任何内部匹配.我正在寻找类似的东西:
->whereIn('country', 'city', $countries, $cities)
Run Code Online (Sandbox Code Playgroud)
......"国家"是美元国家的地方,"城市"是美元城市的地方.
我可以把两者分开,但是所需的查询有很多其他条件,所以这是不可能的.我之后得到的SQL将是:
SELECT ...
WHERE ...
AND ...
AND ...
AND ('country' IN (1,2,3) OR 'city' IN (7,8,9))
AND ...
AND ...;
Run Code Online (Sandbox Code Playgroud)
因此,人们可以认为它也只是一个包围问题.有人知道Doctrine DQL是否可以实现这一点?我查看了文档,但找不到任何方向.
谢谢
where-clause ×10
haskell ×3
sql-server ×3
t-sql ×3
case ×1
conditional ×1
database ×1
doctrine ×1
dql ×1
dynamic ×1
guard-clause ×1
if-statement ×1
not-exists ×1
parse-error ×1
performance ×1
sql ×1
sql-update ×1
symfony1 ×1
syntax ×1
types ×1