MySQL查询索引中的所有搜索词
我有一张员工及其日程表,如下所示:
Emp_Name | Date
-------- -----
Smith | 08-01-2009
Jones | 08-01-2009
Goodman | 08-02-2009
Smith | 08-02-2009
Jones | 08-02-2009
Goodman | 08-03-2009
我将如何编写查询,以便结果仅是在 08-02-2009
和 08-03 工作的员工的员工姓名-2009。
我陷入困境,因为我能想到的只是获取任一匹配项的名称的方法,但我似乎找不到正确的方法来获取仅与多个搜索条件的所有搜索条件相匹配的名称的结果行。
因此,根据示例条件,我应该只选择古德曼。但是,如果我执行像 WHERE Date IS(日期列表)
这样的查询,我会得到 Goodman 和 Smith 和 Jones,因为 Smith 和 Jones 的工作时间都是 08-02-2009。如果我尝试进行某种连接,我不会总是得到相同的列,因为每个员工工作的天数是可变的。
我认为 Union 可能是可行的方法,但我并不总是知道某人搜索的条件如何。
I have a table of employees and their schedule, like so:
Emp_Name | Date
-------- -----
Smith | 08-01-2009
Jones | 08-01-2009
Goodman | 08-02-2009
Smith | 08-02-2009
Jones | 08-02-2009
Goodman | 08-03-2009
How would I write a query so that the results were only employee names of employees working on 08-02-2009
and 08-03-2009
.
I'm getting caught up because all I can think of are ways to get the names for EITHER match, but I can't seem to find the right way to get the results for only the names that have matches for all search criteria across multiple rows.
So based on the example conditions, I should only get Goodman. But if I do a query like WHERE Date IS (list of dates)
I would get Goodman and Smith and Jones, since Smith and Jones both work on 08-02-2009. If I try to do some kind of JOIN, I would not always end up with equal columns, since the number of days each employees works is variable.
I thought Union might be the way to go, but I wouldn't always know how may conditions someone was searching by.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不需要通用解决方案,可以使用:
If you don't need a generic solution, you can use this:
这是我的第一次尝试,可能有比使用 HAVING 更有效的方法......
Here's my first stab at it, there's probably a more efficient way than using HAVING though...