![](/img/trans.png)
[英]MySQL Left Joins: Select everything from one table, but only matching value in second table with criteria
[英]MySQL - Select everything from one table, but only first matching value in second table
在MySQL中创建查询我感觉有些生疏。 我以为我可以解决这个问题,但我没有运气,四处寻找不会产生类似的东西......
基本上,我有两张桌子。 我想从一个表和第二个表中的匹配行中选择所有内容。 但是,我只想从第二个表中得到第一个结果。 我希望这是有道理的。
daily_entries
表中的行是唯一的。 每天会有一排,但可能不是每天都有。 第二个表notes
中包含许多行,其每一个与一个行相关联的从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
,但是只返回每个匹配集的第一行。 甚至可以这样做吗? 我非常感谢有人能提供的任何帮助。 在我的查询结束时使用Limit
显然将其限制为我指定的值,但将其应用于预期的所有内容。
基本上,您的查询没有任何问题。 我相信它正是您所需要的,因为它返回您想要的数据。 您不能将它看作是复制您应该查看它的daily_entries
,就像它返回所有带有关联daily_entry
notes
daily_entry
。
当然,你可以实现你在问题中描述的内容(已经有一个解决这个问题的答案),但在你这样做之前要三思而后行,因为这样的嵌套查询只会给你的数据库服务器增加很多明显的性能开销。
我建议使用单个LEFT JOIN
(这是您所需要的)尽可能简单地查询,然后让消费应用程序操纵数据并以他们需要的方式呈现它。
按功能使用mysql的非标准组:
SELECT n.notes, n.task_id, de.date, de.location, de.eid
FROM notes n
LEFT JOIN (select * from
(select * from daily_entries ORDER BY date DESC) x
group by eid) de ON de.eid = n.eid
您需要使用最后一行的显式过滤来执行这些查询。 此示例使用join
执行此操作:
SELECT n.notes, n.task_id, de.date, de.location, de.eid
FROM daily_entries de LEFT JOIN
notes n
ON de.eid = n.eid LEFT JOIN
(select n.eid, min(task_id) as min_task_id
from notes n
group by n.eid
) nmin
on n.task_id = nmin.min_task_id
ORDER BY de.date DESC;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.