我在MySQL中创建查询感觉有点生疏。我以为我可以解决这个问题,但我没有运气,四处搜寻不会导致任何类似的...MySQL - 从一个表中选择所有内容,但只在第二个表中选择第一个匹配值
基本上,我有两个表。我想从一个表中选择一切,并从第二个表中选择匹配的行。但是,我只想从第二个表获得第一个结果。我希望这是有道理的。
daily_entries
表中的行是唯一的。每天会有一排,但可能不是每天。第二个表notes
包含许多行,其中每行都与ONE行相关联,从daily_entries
开始。
下面是我的表格的例子;
表一
mysql> desc daily_entries;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| eid | int(11) | NO | PRI | NULL | auto_increment |
| date | date | NO | | NULL | |
| location | varchar(100) | NO | | NULL | |
+----------+--------------+------+-----+---------+----------------+
表二
mysql> desc notes;
+---------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+----------------+
| task_id | int(11) | NO | PRI | NULL | auto_increment |
| eid | int(11) | NO | MUL | NULL | |
| notes | text | YES | | NULL | |
+---------+---------+------+-----+---------+----------------+
我需要做的,是从notes
选择所有条目,从daily_entries
只有一个结果。
下面是我多么希望它看起来一个例子:
+----------------------------------------------+---------+------------+----------+-----+
| notes | task_id | date | location | eid |
+----------------------------------------------+---------+------------+----------+-----+
| Another note | 3 | 2014-01-02 | Home | 2 |
| Enter a note. | 1 | 2014-01-01 | Away | 1 |
| This is a test note. To see what happens. | 2 | | Away | 1 |
| Testing another note | 4 | | Away | 1 |
+----------------------------------------------+---------+------------+----------+-----+
4 rows in set (0.00 sec)
下面是查询我目前有:
SELECT notes.notes, notes.task_id, daily_entries.date, daily_entries.location, daily_entries.eid
FROM daily_entries
LEFT JOIN notes ON daily_entries.eid=notes.eid
ORDER BY daily_entries.date DESC
下面是它的外观与我的查询为例:
+----------------------------------------------+---------+------------+----------+-----+
| notes | task_id | date | location | eid |
+----------------------------------------------+---------+------------+----------+-----+
| Another note | 3 | 2014-01-02 | Home | 2 |
| Enter a note. | 1 | 2014-01-01 | Away | 1 |
| This is a test note. To see what happens. | 2 | 2014-01-01 | Away | 1 |
| Testing another note | 4 | 2014-01-01 | Away | 1 |
+----------------------------------------------+---------+------------+----------+-----+
4 rows in set (0.00 sec)
起初,我以为我可以简单地GROUP BY daily_entries.date
,但是这回ONL y每个匹配集的第一行。这甚至可以完成?我将不胜感激任何人可以提供的帮助。在我的查询结束处使用Limit
显然将其限制为我指定的值,但将其应用于所需的所有内容。
通常这些类型的格式的改进在应用层实现(例如,报告工具有一个选项,“禁止重复值”) – cha
你能确认你想要第二张表中最近的一行(如果存在)? – Bohemian