Mysql 在SQL查询中使用Distinct
请查找下面给出的查询:Mysql 在SQL查询中使用Distinct,mysql,sqlite,Mysql,Sqlite,请查找下面给出的查询: SELECT DISTINCT reco_index_content_code, reco_index_content_name, reco_index_content_url FROM tbl_reco_index_contents WHERE reco_index_user_action_at_select = 1 AND user_profile_number = 1
SELECT DISTINCT
reco_index_content_code,
reco_index_content_name,
reco_index_content_url
FROM tbl_reco_index_contents
WHERE
reco_index_user_action_at_select = 1
AND user_profile_number = 1
我需要选择reco\u index\u content\u name作为distinct
为了实现这一点,应该如何修改上述查询,以确保没有重复的记录索引内容名称行?标准解决方案记录在案,并使用不相关的子查询,如下所示:
SELECT x.*
FROM my_table x
JOIN
( SELECT grouping_id
, MIN(ordering_id) min_ordering_id
FROM my_table
GROUP
BY grouping_id
) y
ON y.grouping_id = x.grouping_id
AND y.min_ordering_id = x.ordering_id;
您将如何处理以下记录索引内容代码?例如,只选择最低的代码?如果有重复的代码,您希望返回哪一行。请注意,“不在乎”通常表示设计拙劣。@草莓-我理解,但我这样做只是为了临时修复演示。它可以是任意行。那么它可以是任意值吗?为其他列使用常量:选择DISTINCT作为记录索引内容代码,记录索引内容名称,记录索引内容url从…您能告诉我如何将其应用于我的上述查询吗?对不起,我在这里没有太多的专业知识。也许没有,但我想你能弄明白-好啊my_table=tbl_reco_index_contents,grouping_id=reco_index_content_code。什么是排序id?根据您的描述,分组id是记录索引内容名称