gpt4 book ai didi

MySQL 查询没有产生正确的结果

转载 作者:行者123 更新时间:2023-11-28 23:38:15 25 4
gpt4 key购买 nike

鉴于此:

MariaDB [master]> describe history;
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| historyid | int(10) unsigned | NO | PRI | NULL | auto_increment |
| amount | float | NO | | NULL | |
| subsidy | char(1) | NO | | NULL | |
| last_payment | date | NO | | NULL | |
| amount_paid | float | NO | | NULL | |
| balance | float | NO | | NULL | |
| attend | char(1) | NO | | N | |
| attend_date | date | NO | | NULL | |
| groupid | int(11) unsigned | NO | | NULL | |
| clientid | int(10) unsigned | NO | MUL | NULL | |
| memberid | int(10) unsigned | NO | MUL | NULL | |
+--------------+------------------+------+-----+---------+----------------+


MariaDB [master]> describe participation;
+-----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------------+------+-----+---------+----------------+
| partid | int(11) | NO | PRI | NULL | auto_increment |
| notes | varchar(255) | NO | | NULL | |
| groupdate | date | NO | | NULL | |
| clientid | int(10) unsigned | NO | MUL | NULL | |
| memberid | int(10) unsigned | NO | MUL | NULL | |
+-----------+------------------+------+-----+---------+----------------+

MariaDB [master]> select * from participation;
+--------+-----------+------------+----------+----------+
| partid | notes | groupdate | clientid | memberid |
+--------+-----------+------------+----------+----------+
| 194 | test test | 2016-01-26 | 3 | 1 |
+--------+-----------+------------+----------+----------+

如何编写以下查询

MariaDB [master]> SELECT attend_date, groupdate, h.clientid, h.memberid
-> FROM history AS h
-> LEFT JOIN participation AS p ON groupdate = attend_date
-> WHERE h.memberid = "1"
-> AND MONTH(attend_date) = "1"
-> AND YEAR(attend_date) = "2016"
-> AND attend_date <> "0000-00-00"
-> ORDER BY attend_date ASC;
+-------------+------------+----------+----------+
| attend_date | groupdate | clientid | memberid |
+-------------+------------+----------+----------+
| 2016-01-26 | 2016-01-26 | 3 | 1 |
| 2016-01-26 | 2016-01-26 | 4 | 1 |
| 2016-01-26 | 2016-01-26 | 1 | 1 |
| 2016-01-26 | 2016-01-26 | 2 | 1 |
| 2016-01-28 | NULL | 3 | 1 |
| 2016-01-28 | NULL | 4 | 1 |
| 2016-01-28 | NULL | 1 | 1 |
| 2016-01-28 | NULL | 2 | 1 |
+-------------+------------+----------+----------+

所以我的返回是这样的

+-------------+------------+----------+----------+
| attend_date | groupdate | clientid | memberid |
+-------------+------------+----------+----------+
| 2016-01-26 | 2016-01-26 | 3 | 1 |
| 2016-01-26 | NULL | 4 | 1 |
| 2016-01-26 | NULL | 1 | 1 |
| 2016-01-26 | NULL | 2 | 1 |
| 2016-01-28 | NULL | 3 | 1 |
| 2016-01-28 | NULL | 4 | 1 |
| 2016-01-28 | NULL | 1 | 1 |
| 2016-01-28 | NULL | 2 | 1 |
+-------------+------------+----------+----------+

这里的整个想法是能够拉动 所有 尚未将 notes 应用到 groupdate 匹配 历史表的 attend_date。并且只有在发布注释时才会将 groupdate 设置为 attend_date。然而,正如我所知道的那样,查询为所有四个 clients 返回 groupdate2016-01-26 而不仅仅是一个。在此先感谢您的帮助。

最佳答案

查看您的表结构,我看到您的 attend_date 和您尝试加入的 groupdate 之间的关系,但我还看到那些表有 clientidmemberid 相同。

由于您仅在日期加入,因此您将返回该条件下所有客户和所有日期的笛卡尔积。您可以通过使用逻辑 AND 添加附加条件来使连接的 ON 子句更加具体。

LEFT JOIN participation AS p
ON groupdate = attend_date
AND h.clientid = p.clientid

如果你想要的结果集也需要一个条件来匹配这些表之间的 memberid 对我来说不是很明显,但如果是这样,解决方案就像另一个 AND 一样简单

LEFT JOIN participation AS p
ON groupdate = attend_date
AND h.memberid = p.memberid

不要将连接的 ON 子句仅仅看作是公共(public)列之间的匹配,尽管这是它最常见的使用方式。相反,ON 子句只需要提供一些条件,当 true 导致返回连接的行时(就像您习惯使用 WHERE 子句一样)。这意味着您可以在那里放置任何可以评估为真或假的东西,从而允许复杂的连接逻辑。 ORDER BYGROUP BY 也是如此。

关于MySQL 查询没有产生正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35186564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com