简体   繁体   English

搜索具有相同值的多行sql

[英]search multiple rows with the same values sql

I have a table in my database which has the following structure: 我的数据库中有一个表,其结构如下:

ID | Date | Ball1 | Ball2 | Ball3 | Ball4 | Ball5 | Ball6 | Ball7 | BonusBall

All the Ball rows are just integers. 所有Ball行都是整数。

What I want to do now is to write a select query that selects all the rows that have multiple of the same values in it. 我现在要做的是编写一个选择查询,该查询选择其中具有多个相同值的所有行。

For instance all the rows that contain 1 and 12. 例如,所有包含1和12的行。

I tried the following query, but that doesn't quite give my what I want: 我尝试了以下查询,但这并不能完全满足我的需求:

SELECT * FROM number 
WHERE ball1 OR ball2 OR ball3 OR ball4 OR ball5 OR ball6 OR bonusball IN (1,12);

Am I on the right track? 我在正确的轨道上吗? Any suggestions? 有什么建议么?

Your solution was already pretty good, but if you want to check it for each field, I believe you have to write it like this: 您的解决方案已经相当不错,但是如果您想针对每个字段进行检查,我相信您必须像这样编写:

SELECT * FROM test
WHERE (ball1 = 1 OR ball2 = 1 OR ball3  = 1  
OR ball4  = 1 OR ball5 = 1 OR ball6  = 1 OR bonusball = 1)
AND (ball1 = 12 OR ball2 = 12 OR ball3 = 12  
OR ball4 = 12 OR ball5 = 12 OR ball6 = 12 OR bonusball = 12);

Although I would not advise running this query in a high traffic environment. 尽管我不建议在高流量环境中运行此查询。 A better idea would be to have two tables. 一个更好的主意是拥有两个表。 One containing 'Lottery' and the other one 'Balls'. 一个包含“彩票”,另一个包含“球”。 Where Balls would have a reference to the lottery table. Balls会参考彩票表。

You should try to read up on Database normalization . 您应该尝试阅读数据库规范化

first of all - you should practice 'normalization' 首先-您应该练习“规范化”

second a better syntax would be 其次,更好的语法是

SELECT * 
FROM number 
WHERE ball1 in (1,12) 
OR ball2 in (1,12) 
OR ball3 in (1,12) 

etc 等等

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

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