MySQL查询索引中的所有搜索词

发布于 2024-08-02 20:12:30 字数 609 浏览 1 评论 0原文

我有一张员工及其日程表,如下所示:

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-200908-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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不甘平庸 2024-08-09 20:12:31

如果您不需要通用解决方案,可以使用:

select Emp_Name
from employee
where Emp_Name
  in (select Emp_Name from employee where Date = '08-02-2009')
  and in (select Emp_Name from employee where Date = '08-03-2009')
  ...

If you don't need a generic solution, you can use this:

select Emp_Name
from employee
where Emp_Name
  in (select Emp_Name from employee where Date = '08-02-2009')
  and in (select Emp_Name from employee where Date = '08-03-2009')
  ...
硬不硬你别怂 2024-08-09 20:12:30

这是我的第一次尝试,可能有比使用 HAVING 更有效的方法......

SELECT Emp_Name,COUNT(DISTINCT date) as daycount 
FROM employee 
WHERE date IN ('08-01-2009', '08-02-2009')
GROUP BY Emp_Name
HAVING daycount=2 

Here's my first stab at it, there's probably a more efficient way than using HAVING though...

SELECT Emp_Name,COUNT(DISTINCT date) as daycount 
FROM employee 
WHERE date IN ('08-01-2009', '08-02-2009')
GROUP BY Emp_Name
HAVING daycount=2 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文