简体   繁体   English

选择结果多列

[英]Select the result where multiple columns

I need to select the result with check the multiple columns. 我需要选择结果并检查多个列。

 SELECT * FROM atricle WHERE  
    a.article_free_1 = 1 AND  
    a.article_free_2 = 1 AND 
    a.article_free_3 = 1 AND 
    a.article_free_4 = 1 AND 
    a.article_free_5 = 1 AND 
    a.article_free_6 = 1 AND 
    a.article_free_7 = 1 AND 
    a.article_free_8 = 1 AND 
    a.article_free_9 = 1 AND 
    a.article_free_10 = 1;

Here I want to simplify the query.Its going very long and I need to add 40+ columns in my query. 在这里我想简化查询,它会花很长时间,并且需要在查询中添加40多个列。

How to simplify my query? 如何简化查询?

If you are using this query very frequently then its better to create a calculated/computed column in the table. 如果您经常使用此查询,则最好在表中创建一个计算/计算列。 This will be like this: 这将是这样的:

(CASE WHEN article_free_1 = 1 
       AND article_free_2 = 1 AND ....THEN 1 ELSE 0 END)

you can replace the query with : 您可以将查询替换为:

SELECT * FROM atricle WHERE <computedColumn> = 1

it should simplify the query and give you optimized result eachtime you execute it. 它应该简化查询并在每次执行时为您提供优化的结果。

I can't simply comment yet so I'll post as an answer. 我不能简单地发表评论,所以我将其发布为答案。 Your query is as optimal as it can get. 您的查询是最佳状态。 A calculated field will just add overhead, getting in the way of the query optimizer trying to evaluate. 计算字段只会增加开销,从而妨碍查询优化器尝试评估。 Whatever you do, DON'T loop in sql, it was a horrendous addition way back trying to make people like SQL. 无论您做什么,都不要在sql中循环,这是试图使人们喜欢SQL的一种可怕的附加方式。 Stick to standard queries. 坚持标准查询。 What you got is good. 你得到的是好的。

-Edit I jus read what Jeemusu wrote. 编辑我读过Jeemusu写的东西。 spot on. 发现。

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

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