简体   繁体   English

SQLLite SQL语句查找多个列的第一个非零

[英]SQLLite SQL statement to find first non-zero of multiple columns

I have 2 rows in a table. 我在表中有2行。 Each row contains a primary key, and 5 additional columns, whose value may be 1, or 0. 每行包含一个主键和5个其他列,其值可以为1或0。

So for example one of the columns is "TermsAccepted" and another is "CopyrightAcknowledged" 例如,一列是“ TermsAccepted”,另一列是“ CopyrightAcknowledged”

Row 1's "Terms Accepted" might be 0 or 1, and row 2's "Terms Accepted" might be 0 or 1 as well. 第1行的“接受的条款”可能为0或1,第2行的“接受的条款”也可能为0或1。

I'd like to know if at least one of the rows contains a 1 for each column. 我想知道每一行中是否至少有一行包含1。 Do I have to use 5 subqueries to do this (I'd like to just have one row returned that tells me if any of the columns have 1's in them). 我是否必须使用5个子查询来执行此操作(我只想返回一行来告诉我其中是否有1列)。 It doesn't matter to me how many had 1, just so long as at least one column did. 只要有至少一列,对我来说多少就没有关系。

Hopefully that made sense. 希望这是有道理的。 How could I write a query to do this? 我如何编写查询来做到这一点? I'm using SQL Lite. 我正在使用SQL Lite。 I'm somewhat stuck. 我有些卡住。 Thanks so much! 非常感谢!

Note: I can just return both records, and then use Swift (the programming language i'm doing this in) to just parse each column in each row and look for a 0 or a 1, but I was hoping for something in SQL to make it more elegant if possible. 注意:我可以只返回两条记录,然后使用Swift(我正在使用的编程语言)来解析每一行中的每一列并寻找0或1,但是我希望在SQL中找到一些东西如果可能的话,使其更优雅。

I think this should work but if anyone could confirm that would be great, thanks! 我认为这应该可行,但是如果有人可以确认那会很好,谢谢!

SELECT 
        max(TermsAccepted), 
        max(CopyrightAcknowledged), 
        max(LoggedIn), 
        max(LockedOut), 
        max(AccountCopied)
FROM MyTable

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

相关问题 四舍五入到第一个非零值 - Rounding float up to first non-zero value 在sqllite中向表中添加多个列 - Adding multiple columns to a table in sqllite 最初使用非零值调用numberOfRowsInSection - numberOfRowsInSection initially called with non-Zero value 错误 npm 以非零代码退出:1 expo React Native - Error npm exited with non-zero code: 1 expo React Native 用非零起点初始化UIImageView的更好方法 - Better way to initialize UIImageView with non-zero origin 格式加倍到最大非零有效数字 - Format Double up to Max Non-Zero Significant Digits 当iPad上的reloadData从非零部分变为零部分时,UITableViewController似乎没有刷新 - UITableViewController doesn't seem to refresh when reloadData goes from non-zero to zero sections on iPad 将小数点格式设置为最后一个零后有四个非零数字 - Format decimals to have four non-zero digits after final zero 核心数据EXC_BAD_ACCESS用于非零整数值 - Core Data EXC_BAD_ACCESS for non-zero integer values Fastlane upload_to_testflight 对 iTMSTransporter 的调用以非零退出状态完成:1。这表示失败 - Fastlane upload_to_testflight The call to the iTMSTransporter completed with a non-zero exit status: 1. This indicates a failure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM