我有一个桌面。在此表中,我有记录页面表,其中一些页面具有重复的条目。我想基于city_id从表中获取唯一页面。我执行此查询的地方: -
SELECT distinct pagename FROM tbl_page
WHERE status = '0' AND city_id = '78' order by pagename
此查询将不同的记录返回2608.我想在我的查询中获取此页面的id和section id。所以当我执行这个查询时
SELECT distinct pagename,id,section_id FROM tbl_page
WHERE status = '0' AND city_id = '78' order by pagename
此时它没有返回不同的值。它返回了3400个重复的记录。谁能告诉我我做错了什么? 任何帮助将不胜感激。在此先感谢。
答案 0 :(得分:1)
我真的不知道你期望的结果,但也许这就是你想要的:
SELECT pagename,id,section_id FROM tbl_page
WHERE status = '0' AND city_id = '78'
GROUP BY pagename, id, section
order by pagename
如果你想拥有独特的网页名,你应该:
SELECT pagename,id,section_id FROM tbl_page
WHERE status = '0' AND city_id = '78'
GROUP BY pagename
order by pagename
但是你可能对一个页面名称有不同的id / section_id值,所以我认为id和section_id的值将是找到页面名称的第一行的值。
答案 1 :(得分:1)
DISTINCT将只返回不同的行,因此,您也可以切换到GROUP BY。
SELECT `pagename`, `id`, `section_id` FROM `tbl_page`
WHERE status = '0' AND city_id = '78'
GROUP BY 'pagename', 'section_id', `id`
ORDER BY `pagename` ASC
答案 2 :(得分:0)
DISTINCT始终适用于单个列。或者,您可以使用GROUP BY关键字,该关键字可用于多个列。请参阅以下查询:
SELECT pagename,id,section_id FROM tbl_page WHERE status = '0' AND city_id = '78' GROUP BY pagename,id,section_id ORDER BY pagename
答案 3 :(得分:0)
尝试这可能会解决: 您需要使用不同的完整行,就像这样:
SELECT pagename,id,section_id,DISTINCT(*) FROM tbl_page WHERE status = '0' AND city_id = '78' GROUP BY pagename,id,section_id ORDER BY pagename