[英]SQL SELECT only field where it contains words
I'm interested in doing something like in this question , where I only get the result, if the column actually contains all the words given to it. 我有兴趣在此问题中执行类似的操作,如果该列实际上包含给出的所有单词,则只能得到结果。
For example, if I have two columns, one being a timestamp, the other being the weather, I want to get only the timestamps where the weather was cloudy
, rainy
, snowy
, and not if it was only cloudy
, rainy
. 例如,如果我有两列,一列是时间戳,另一列是天气,我只想获取天气
cloudy
, rainy
, snowy
的时间戳,而不是仅cloudy
, rainy
。 An example of the above would be sort of like: 上面的例子有点像:
SELECT distinct timestamp FROM mytable
WHERE Weather LIKE 'snowy'
OR Weather LIKE 'cloudy'
OR Weather LIKE 'rainy'
Which would return something like: 这将返回类似:
2017-09-22 snowy
2017-09-22 snowy
2017-09-22 cloudy
2017-09-22 rainy
2017-09-22 cloudy
But not any results from 2017-09-21
, because it might only have been snowy
and rainy
that day etc. 但从
from 2017-09-21
没有任何结果,因为from 2017-09-21
可能只有snowy
和rainy
等。
The example query I did above would return all timestamps, as long as the weather matched the criteria. 我上面做的示例查询将返回所有时间戳,只要天气符合条件即可。
You can use group by
and having
. 您可以使用
group by
并having
。 I would recommend: 我建议:
SELECT timestamp
FROM mytable
WHERE Weather IN ('snowy', 'cloudy', 'rainy')
GROUP BY timestamp
HAVING COUNT(DISTINCT Weather) = 3; -- get all of them
If you have no duplicates for timestamp
/ Weather
in the table, then use COUNT(*) = 3
instead of COUNT(DISTINCT)
. 如果表中没有
timestamp
/ Weather
重复项,则使用COUNT(*) = 3
而不是COUNT(DISTINCT)
。
You can use like bellow , 您可以像下面这样使用
SELECT timestamp FROM mytable WHERE Weather IN ('snowy', 'cloudy', 'rainy') 从mytable WHERE Weather IN(“ snowy”,“ cloudy”,“ rainy”)中选择时间戳
Output : example 输出:示例
1 1个
2 2
3 3
Dummy table 假人桌
1 snowy 1下雪了
2 cloudy 2多云
3 rainy 3多雨
4 aaaa 4 aaaa
5 bbbb 5 bbbb
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.