mysql sql distinct with join 两个表

标签 mysql sql join distinct

我有两个 mysql 表,表 event 和表 eventvenue

我想从 field 获取其他事件。

------------表事件:----------------

id_e
id_event
title
content

------------ 表事件地点:------------

id_ev
id_event_ev
id_location_ev

我使用 join 从 field 获取其他事件:

SELECT * 
FROM eventvenue 
JOIN event ON eventvenue.id_event_ev = event.id_event 
WHERE eventvenue.id_location_ev=1

结果是:

id_ev  id_event_ev  id_location_ev  id_e  id_event  title   content  
-----  -----------  --------------  ----  --------  ------  ------------
1      2            1               2     2         eventA  aaaaaaaaaaaa
2      2            1               2     2         eventA  aaaaaaaaaaaa
1      4            1               4     4         eventB  bbbbbbbbb
1      9            1               9     9         eventC  cccccccc
3      5            1               5     5         event5  555555

输出包含具有相同 id_event_ev/id_event2 的两行。

我如何才能只显示每个位置的不同 事件 ID?像这样:

id_ev  id_event_ev  id_location_ev  id_e  id_event  title   content  
-----  -----------  --------------  ----  --------  ------  ------------
1      2            1               2     2         eventA  aaaaaaaaaaaa
1      4            1               4     4         eventB  bbbbbbbbb
1      9            1               9     9         eventC  cccccccc
3      5            1               5     5         event5  555555

注意:id_ev不重要。

最佳答案

试试这段代码

SELECT * 
FROM eventvenue 
JOIN event ON eventvenue.id_event_ev = event.id_event 
WHERE eventvenue.id_location_ev=1
GROUP BY event.id_event, eventvenue.id_event_ev

关于mysql sql distinct with join 两个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10580853/

相关文章:

php - 如何显示多张图片上传中的多张图片

sql - CAST、SUM、CASE 问题

sql - 连接查询中的表顺序

SQL 连接 : Select status of reviews submitted by employees and also the list of employees who have not submitted the review for each year

mysql - 使用 GROUP BY/HAVING,但随后扩展结果

mysql - 如何在mysql/zend框架中存储和处理货币

mysql - 组合多个更新语句

java - H2 数据库上未执行 Sql 查询

PHP 查询 - 帮助将两个查询链接到 1 个 echo 语句

mysql - 表达式不在 GROUP BY 子句中