在同一列上使用多个 WHERE 条件进行 SELECTING
好吧,我想我可能在这里忽略了一些明显/简单的东西...但是我需要编写一个查询,仅返回与同一列上的多个条件匹配的记录...
我的表是一个非常简单的链接设置,用于将标志应用到用户...
ID contactid flag flag_type
-----------------------------------
118 99 Volunteer 1
119 99 Uploaded 2
120 100 Via Import 3
121 100 Volunteer 1
122 100 Uploaded 2
等等...在这种情况下,您会看到联系人 99 和 100 都被标记为“志愿者”和“已上传”...
我需要做的是返回这些 contactid 的唯一匹配通过搜索表单输入多个条件... contactid 必须匹配所有选定的标志... 在我的脑海中,SQL 应该类似于:
SELECT contactid
WHERE flag = 'Volunteer'
AND flag = 'Uploaded'...
但是... 不返回任何内容... 我在这里做错了什么?
Ok, I think I might be overlooking something obvious/simple here... but I need to write a query that returns only records that match multiple criteria on the same column...
My table is a very simple linking setup for applying flags to a user ...
ID contactid flag flag_type
-----------------------------------
118 99 Volunteer 1
119 99 Uploaded 2
120 100 Via Import 3
121 100 Volunteer 1
122 100 Uploaded 2
etc... in this case you'll see both contact 99 and 100 are flagged as both "Volunteer" and "Uploaded"...
What I need to be able to do is return those contactid's ONLY that match multiple criteria entered via a search form...the contactid's have to match ALL chosen flags... in my head the SQL should look something like:
SELECT contactid
WHERE flag = 'Volunteer'
AND flag = 'Uploaded'...
but... that returns nothing... What am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
您可以使用
GROUP BY
和HAVING COUNT(*) = _
:(假设
contact_id, flag
是唯一的)。或者使用连接:
如果标志列表很长并且有很多匹配项,第一个可能会更快。如果标志列表很短并且匹配项很少,您可能会发现第二个更快。如果性能是一个问题,请尝试对您的数据进行测试,看看哪个效果最好。
You can either use
GROUP BY
andHAVING COUNT(*) = _
:(assuming
contact_id, flag
is unique).Or use joins:
If the list of flags is very long and there are lots of matches the first is probably faster. If the list of flags is short and there are few matches, you will probably find that the second is faster. If performance is a concern try testing both on your data to see which works best.
使用:
关键是
t.flag
的计数需要等于IN
子句中的参数数量。使用
COUNT(DISTINCT t.flag)
是为了防止 contactid 和 flag 的组合没有唯一约束 - 如果没有重复的机会,您可以在查询中省略 DISTINCT :Use:
The key thing is that the counting of
t.flag
needs to equal the number of arguments in theIN
clause.The use of
COUNT(DISTINCT t.flag)
is in case there isn't a unique constraint on the combination of contactid and flag -- if there's no chance of duplicates you can omit the DISTINCT from the query:考虑像这样使用 INTERSECT:
我认为它是最符合逻辑的解决方案。
Consider using INTERSECT like this:
I think it it the most logistic solution.
无法真正看到您的桌子,但标志不能同时是“志愿者”和“已上传”。如果列中有多个值,则可以使用
不太适用的格式查看表格。
can't really see your table, but flag cannot be both 'Volunteer' and 'Uploaded'. If you have multiple values in a column, you can use
not really applicable seeing the formatted table.
尝试使用这个替代查询:
Try to use this alternate query:
像这样的东西应该适合你
something like this should work for you
将 AND 更改为 OR。简单的错误。把它想象成简单的英语,我想选择任何与这个或那个相等的东西。
Change AND to OR. Simple mistake. Think of it like plain English, I want to select anything with that equals this or that.
仅当您的列中同时存在
volunteer
和uploaded
时,AND
才会返回答案。否则它将返回null
值...尝试在语句中使用
OR
...AND
will return you an answer only when bothvolunteer
anduploaded
are present in your column. Otherwise it will returnnull
value...try using
OR
in your statement ...使用这个:
例如:
Use this:
For example:
有时你只见树木不见森林:)
你原来的SQL..
应该是:
Sometimes you can't see the wood for the trees :)
Your original SQL ..
Should be: