简体   繁体   English

SQL后备或C#数据表

[英]SQL fallback or c# datatable

say I have ten cars 说我有十辆车

columns for color, engine_size, type, make... etc there is 50 columns 颜色,engine_size,类型,make ...等列,共有50列

I want to select for example: 我想选择例如:

SELECT * FROM CARS WHERE COLOR = 'red' AND ENGINE_SIZE = 'V8' AND TYPE = 'coupe' and MAKE = 'Ford' etc for all 50 columns... 从所有颜色为50列的汽车中选择*颜色='红色'AND ENGINE_SIZE ='V8'且类型='轿跑车'和MAKE ='福特'的汽车...

but... and this where I'm stuck for the last week... I do not want the whole SELECT statement to come back empty when a condition is not met... I want it to fallback to whatever it could get... 但是...这就是我上周遇到的问题...我不希望整个SELECT语句在不满足条件时恢复为空...我希望它回退到可能达到的水平。 ..

for example if it only finds cars that are red and are Ford coupes but none are V8s then instead of coming back empty I want it to return the records that are red and coupes and Ford... and I have to do that for all 50 columns... 例如,如果它只找到红色且是福特双门轿跑车的汽车,但没有找到V8,则与其返回空,我要它返回红色,双门轿跑车和福特的记录...而我必须对所有50个汽车都这样做列...

After trying and googling for a week I realise that SQL has no way to alter itself on the fly... you make a SELECT statement then it goes off and runs it... it has no way to go okay that part of the statement returns empty so I'll fall back to the bit that did work.. 经过一个星期的尝试和谷歌搜索之后,我意识到SQL无法即时更改自身...您执行SELECT语句然后关闭并运行它...它无法执行该语句的那部分返回空值,所以我将退回到起作用的位。

So now I'm thinking I need to do it with c# and write some ghastly code that keeps iterating through a datatable over and over gradually reducing it until it leaves what it could find. 所以现在我想我需要使用c#并编写一些可怕的代码,这些代码不断地遍历数据表,并逐步减少它,直到它无法找到为止。

This does not seem elegant and I'm wondering if there is a better way? 这似乎并不优雅,我想知道是否有更好的方法? I have not posted any of my sample code because nothing I've tried (ISNULL, IF EXISTS, etc) does anything close to what I want to achieve. 我没有发布任何示例代码,因为我尝试过的任何事情(ISNULL,IF EXISTS等)都无法实现我想要实现的目标。 I just get back nothing or everything. 我什么也没回来。

To simplify what I'm saying with psuedocode: 为了简化我用psuedocode所说的话:

If result != empty 如果结果!=空

select * where Color = 'red' and Type = 'coupe' and Make = 'Ford' etc (for 50 conditions) 选择*,其中Color ='red'和Type ='coupe'和Make ='Ford'等(对于50种情况)

else 其他

select * where Color = 'red' and Type = 'coupe' and Make = 'Ford' etc (for 49 conditions) 选择*,其中Color ='red'和Type ='coupe'和Make ='Ford'等(对于49种情况)

... etc ...等等

but even this will fail because I would have to test for every possible combination of those 50 conditions... not just 50 one after another 但即使这样也会失败,因为我必须测试这50个条件的每种可能组合...而不仅仅是一个接一个地测试50个条件

This is my first time posting here so my apologies if I haven't explained myself very well. 这是我第一次在这里发布文章,因此如果我对自己的解释不够好,我深表歉意。

You can use order by instead of where : 您可以使用order by代替where

select top 1 c.*
from cars c
order by ((case when Color = 'red' then 1 else 0 end) + 
          (case when Type = 'coupe' then 1 else 0 end) + 
          (case when Make = 'Ford' then 1 else 0 end) + 
          . . .
         ) desc;

You can, of course, adjust the "1"s to give weightings to the different features. 当然,您可以调整“ 1”以赋予不同功能的权重。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM