mysql 子查询帮助

发布于 2024-11-28 16:13:13 字数 225 浏览 0 评论 0原文

我的 MySql 数据库中有 3 个表:

  1. 用户;
  2. 拥有称为数据的日期字段的公司;
  3. 具有 idUser 和 idCompany 的对象。

我必须检索在公司日期字段中有特定日期的公司,并且满足以下规则: 对象表中不应有 idP=xx 和 idI=(已获取特定起息日期 yy-mm-dd 的公司的 id)的记录

我该如何编写此查询?

I have 3 tables in a MySql Database:

  1. User;
  2. Company that has a date field called data;
  3. Object that has idUser and idCompany.

I have to retrieve Companies that has got a specific date in date field of Company, and that meet the following rule:
There should be no records in Object table that has idP=xx and idI=(an id of a company that has got the specific value date yy-mm-dd)

How can I write this query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

挽袖吟 2024-12-05 16:13:13
SELECT * FROM Company WHERE data = '2011-08-09' AND
Company.id NOT IN
(SELECT Object.idI FROM Object WHERE Object.idI = Company.id AND Object.idP = 'XX')

但我不得不说你的列和表命名需要重新完成。这可能更有意义。

您必须为Object.idI和Object.idP创建索引才能使其运行得更快。

SELECT * FROM Company WHERE data = '2011-08-09' AND
Company.id NOT IN
(SELECT Object.idI FROM Object WHERE Object.idI = Company.id AND Object.idP = 'XX')

But I have to say that your columns and tables naming need to be re-done. It could be a lot more meaningful.

You have to created indexes for Object.idI and Object.idP to make it run quicker.

无人接听 2024-12-05 16:13:13
SELECT * FROM Company
  LEFT JOIN Object ON( Object.idP = xx OR Object.idl = Company.id )
  WHERE Company.date = somedate AND Object.idP IS NULL
SELECT * FROM Company
  LEFT JOIN Object ON( Object.idP = xx OR Object.idl = Company.id )
  WHERE Company.date = somedate AND Object.idP IS NULL
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文