SQL 联合 2 table 没有重复的行
SQL Joint 2 table without duplicate rows
我正在尝试合并 2 个没有重复行的表
Table 1 - modx_site_content
|id|pagetitle|introtext|pub_date|
---------------------------------
|3635| name1 |texttextt|17.02.2015
|3636| name1 |texttextt|18.02.2015
Table 2 - modx_site_tmplvar_contentvalues
|contentid|tmplvarid|value|
---------------------------
| 3635 | 1 |value1
| 3635 | 1 |value2
| 3636 | 1 |value3
我正在努力使所有
|id|title|introtext|publishdate|photo|
--------------------------------------
|3635|name1|texttextt|17.02.2015|value1, value2
|3636|name1|texttextt|18.02.2015|value3
但当前结果显示重复行 ID 3535
|id|title|introtext|publishdate|photo|
--------------------------------------
|3635|name1|texttextt|17.02.2015|value1
|3635|name1|texttextt|17.02.2015|value2
|3636|name1|texttextt|18.02.2015|value3
我目前的 sql 结果是
SELECT
modx_site_content.id,
pagetitle as 'title',
introtext,
pub_date as 'publishdate',
modx_site_tmplvar_contentvalues.value as 'photo'
FROM `modx_site_content`,
`modx_site_tmplvar_contentvalues`
WHERE parent IN (1153,3271)
AND pub_date>0
AND `contentid`= modx_site_content.id
AND `tmplvarid` IN (10, 15, 19)
Order by `pub_date` DESC LIMIT 20
MySQL 有 group_concat 可能有效(取决于数据类型):
SELECT
modx_site_content.id,
pagetitle as 'title',
introtext,
pub_date as 'publishdate',
group_concat(modx_site_tmplvar_contentvalues.value) as 'photo'
FROM `modx_site_content` JOIN
`modx_site_tmplvar_contentvalues` ON `contentid`= modx_site_content.id
WHERE parent IN (1153,3271)
AND pub_date>0
AND `tmplvarid` IN (10, 15, 19)
GROUP BY modx_site_content.id, pagetitle , introtext, pub_date
解决您当前问题的方法是 group by
和 group_concat()
:
SELECT c.id, c.pagetitle as title, c.introtext, c.pub_date as publishdate,
group_concat(cv.value) as sphotos
FROM `modx_site_content` c JOIN
`modx_site_tmplvar_contentvalues` cv
ON cv.`contentid`= c.id
WHERE c.parent IN (1153, 3271) AND c.pub_date > 0 AND
`tmplvarid` IN (10, 15, 19)
GROUP BY c.id, c.pagetitle, c.introtext, c.pub_date
Order by c.`pub_date` DESC
LIMIT 20;
我还推荐:
- 使用明确的
join
语法。
- 在
from
子句中定义 table 别名。
- 对列引用使用 table 别名。
- 不要使用单引号来定义列别名。您不需要转义字符,所以不要同时使用一个转义字符。
我正在尝试合并 2 个没有重复行的表
Table 1 - modx_site_content
|id|pagetitle|introtext|pub_date|
---------------------------------
|3635| name1 |texttextt|17.02.2015
|3636| name1 |texttextt|18.02.2015
Table 2 - modx_site_tmplvar_contentvalues
|contentid|tmplvarid|value|
---------------------------
| 3635 | 1 |value1
| 3635 | 1 |value2
| 3636 | 1 |value3
我正在努力使所有
|id|title|introtext|publishdate|photo|
--------------------------------------
|3635|name1|texttextt|17.02.2015|value1, value2
|3636|name1|texttextt|18.02.2015|value3
但当前结果显示重复行 ID 3535
|id|title|introtext|publishdate|photo|
--------------------------------------
|3635|name1|texttextt|17.02.2015|value1
|3635|name1|texttextt|17.02.2015|value2
|3636|name1|texttextt|18.02.2015|value3
我目前的 sql 结果是
SELECT
modx_site_content.id,
pagetitle as 'title',
introtext,
pub_date as 'publishdate',
modx_site_tmplvar_contentvalues.value as 'photo'
FROM `modx_site_content`,
`modx_site_tmplvar_contentvalues`
WHERE parent IN (1153,3271)
AND pub_date>0
AND `contentid`= modx_site_content.id
AND `tmplvarid` IN (10, 15, 19)
Order by `pub_date` DESC LIMIT 20
MySQL 有 group_concat 可能有效(取决于数据类型):
SELECT
modx_site_content.id,
pagetitle as 'title',
introtext,
pub_date as 'publishdate',
group_concat(modx_site_tmplvar_contentvalues.value) as 'photo'
FROM `modx_site_content` JOIN
`modx_site_tmplvar_contentvalues` ON `contentid`= modx_site_content.id
WHERE parent IN (1153,3271)
AND pub_date>0
AND `tmplvarid` IN (10, 15, 19)
GROUP BY modx_site_content.id, pagetitle , introtext, pub_date
解决您当前问题的方法是 group by
和 group_concat()
:
SELECT c.id, c.pagetitle as title, c.introtext, c.pub_date as publishdate,
group_concat(cv.value) as sphotos
FROM `modx_site_content` c JOIN
`modx_site_tmplvar_contentvalues` cv
ON cv.`contentid`= c.id
WHERE c.parent IN (1153, 3271) AND c.pub_date > 0 AND
`tmplvarid` IN (10, 15, 19)
GROUP BY c.id, c.pagetitle, c.introtext, c.pub_date
Order by c.`pub_date` DESC
LIMIT 20;
我还推荐:
- 使用明确的
join
语法。 - 在
from
子句中定义 table 别名。 - 对列引用使用 table 别名。
- 不要使用单引号来定义列别名。您不需要转义字符,所以不要同时使用一个转义字符。