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

时间:2016-02-03 20:02:16

标签: mysql

鉴于此:

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匹配history table's {{ 1}}。只有在发布便笺时,attend_date才会设置为groupdate。然而,正如我所知,查询为所有四个attend_date而不是仅一个groupdate返回2016-01-26 clients。在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

查看您的表格结构,我看到您尝试加入的attend_dategroupdate之间的关系,但我也看到这些表格有clientid和{{1共同的。

由于您只加入了该日期,因此您将获得所有客户的笛卡尔产品以及该条件的所有日期。您可以通过添加逻辑memberid的附加条件来使联接的ON子句更具体。

AND

如果您想要的结果集还要求条件在这些表之间匹配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子句只需要提供一些条件,即当返回连接行时返回true(就像你习惯使用ON子句一样)。这意味着你可以放置任何可以被评估为真或假的东西,允许复杂的连接逻辑。 WHEREORDER BY也可以这样说。