SQL未按预期工作

时间:2015-09-16 12:03:06

标签: mysql sql tsql

我在我的SQL查询中遇到了一个我正在尝试运行的问题(我现在正在练习)。我正试图在两个特定日期和一些条件之间获得一些注册表。

问题是,查询无法正常工作,因为它始终只显示相同的注册表,我的意思是,如果有100条目,则查询显示100次相同的条目。

以下是查询:

SELECT Base.Registry.Registry,
       Base.Client.Name,
       Base.Client.Surname1,
       Base.Client.Surname2,
       Base.Client.BirthDate,
       Base.Registry.Edad,
       Base.Client.NIF,
       Base.Registry.Procedency,
       Base.Registry.DateRegistry,
       Base.Registry.DateValidation,
       Base.Registry.CodeSample,
       Base.SnomedRegistry.Code,
       Base.Sample.T
FROM Base.Registry,
     Base.Client,
     Base.SnomedRegistry,
     Base.Sample
WHERE Base.Registry.CodeClient = Base.Client.CodeClient
  AND Base.Registry.Registry = Base.SnomedRegistry.Registry
  AND Base.Registry.CodeSample = Base.Sample.Sample
  AND Base.Registry.DateRegistry BETWEEN '2012-10-26' AND '2012-12-31'
  AND Base.SnomedRegistry.Code LIKE 'M8%%%3'
  OR Base.SnomedRegistry.Code LIKE 'M8%%%2'
  OR Base.SnomedRegistry.Code LIKE 'M8%%%6'
  OR Base.SnomedRegistry.Code LIKE 'M8%%%9'
  OR Base.SnomedRegistry.Code LIKE 'M9%%%2'
  OR Base.SnomedRegistry.Code LIKE 'M9%%%3'
  OR Base.SnomedRegistry.Code LIKE 'M9%%%6'
  OR Base.SnomedRegistry.Code LIKE 'M9%%%9'
  OR Base.SnomedRegistry.Code LIKE 'M69730'
  OR Base.SnomedRegistry.Code LIKE 'M60760'
  OR Base.SnomedRegistry.Code LIKE 'M74000'
  OR Base.SnomedRegistry.Code LIKE 'M74007'
  OR Base.SnomedRegistry.Code LIKE 'M74008'
  LIMIT 0, 100    

我现在对SQL很缺乏经验,所以每个建议都会受到赞赏。

Obs:对不起我的英文。

3 个答案:

答案 0 :(得分:2)

你忘了括号:

SELECT ...
FROM Base.Registry, Base.Client, Base.SnomedRegistry, Base.Sample
WHERE Base.Registry.CodeClient = Base.Client.CodeClient
AND Base.Registry.Registry = Base.SnomedRegistry.Registry
AND Base.Registry.CodeSample = Base.Sample.Sample
AND Base.Registry.DateRegistry
BETWEEN '2012-10-26' AND '2012-12-31'
AND (    #<----- here
   Base.SnomedRegistry.Code LIKE 'M8%%%3'
OR Base.SnomedRegistry.Code LIKE 'M8%%%2'
OR Base.SnomedRegistry.Code LIKE 'M8%%%6'
OR Base.SnomedRegistry.Code LIKE 'M8%%%9'
OR Base.SnomedRegistry.Code LIKE 'M9%%%2'
OR Base.SnomedRegistry.Code LIKE 'M9%%%3'
OR Base.SnomedRegistry.Code LIKE 'M9%%%6'
OR Base.SnomedRegistry.Code LIKE 'M9%%%9'
OR Base.SnomedRegistry.Code LIKE 'M69730'
OR Base.SnomedRegistry.Code LIKE 'M60760'
OR Base.SnomedRegistry.Code LIKE 'M74000'
OR Base.SnomedRegistry.Code LIKE 'M74007'
OR Base.SnomedRegistry.Code LIKE 'M74008' 
)   #<----- here
LIMIT 0 , 100

答案 1 :(得分:2)

我会评论而不是提供答案,但我需要50分才能做到这一点。洛尔

我不太清楚为什么你需要像%%%%%那样多次使用%,因为通常只有一个%就足够了。

我想说你的问题与你的from语句有关,理想情况下需要使用连接。

我也会在你喜欢的陈述中使用括号......

WHERE   1 = 1 
    AND Base.Registry.CodeClient = Base.Client.CodeClient
    AND Base.Registry.Registry = Base.SnomedRegistry.Registry
    AND Base.Registry.CodeSample = Base.Sample.Sample
    AND Base.Registry.DateRegistry BETWEEN '2012-10-26' AND '2012-12-31'
    AND (
            Base.SnomedRegistry.Code LIKE 'M8%3'
            OR Base.SnomedRegistry.Code LIKE 'M8%2'
            OR Base.SnomedRegistry.Code LIKE 'M8%6'
            OR Base.SnomedRegistry.Code LIKE 'M8%9'
            OR Base.SnomedRegistry.Code LIKE 'M9%2'
            OR Base.SnomedRegistry.Code LIKE 'M9%3'
            OR Base.SnomedRegistry.Code LIKE 'M9%6'
            OR Base.SnomedRegistry.Code LIKE 'M9%9'
            OR Base.SnomedRegistry.Code LIKE 'M69730'
            OR Base.SnomedRegistry.Code LIKE 'M60760'
            OR Base.SnomedRegistry.Code LIKE 'M74000'
            OR Base.SnomedRegistry.Code LIKE 'M74007'
            OR Base.SnomedRegistry.Code LIKE 'M74008'
        )
LIMIT 0 , 100

答案 2 :(得分:0)

使用join和括号:

SELECT Base.Registry.Registry, Base.Client.Name, Base.Client.Surname1, Base.Client.Surname2, Base.Client.BirthDate, Base.Registry.Edad, Base.Client.NIF, Base.Registry.Procedency, Base.Registry.DateRegistry, Base.Registry.DateValidation, Base.Registry.CodeSample, Base.SnomedRegistry.Code, Base.Sample.T
FROM Base.Registry
inner join Base.Client on Base.Registry.CodeClient = Base.Client.CodeClient
inner join Base.SnomedRegistry on Base.Registry.Registry = Base.SnomedRegistry.Registry
inner join Base.Sample on Base.Registry.CodeSample = Base.Sample.Sample
WHERE Base.Registry.DateRegistry BETWEEN '2012-10-26' AND '2012-12-31'
AND (Base.SnomedRegistry.Code LIKE 'M8%%%3'
OR Base.SnomedRegistry.Code LIKE 'M8%%%2'
OR Base.SnomedRegistry.Code LIKE 'M8%%%6'
OR Base.SnomedRegistry.Code LIKE 'M8%%%9'
OR Base.SnomedRegistry.Code LIKE 'M9%%%2'
OR Base.SnomedRegistry.Code LIKE 'M9%%%3'
OR Base.SnomedRegistry.Code LIKE 'M9%%%6'
OR Base.SnomedRegistry.Code LIKE 'M9%%%9'
OR Base.SnomedRegistry.Code LIKE 'M69730'
OR Base.SnomedRegistry.Code LIKE 'M60760'
OR Base.SnomedRegistry.Code LIKE 'M74000'
OR Base.SnomedRegistry.Code LIKE 'M74007'
OR Base.SnomedRegistry.Code LIKE 'M74008')
LIMIT 0 , 100